how to convert english date to arabic date ios swift - ios

in my app i am getting date in this format = "2016-02-15 10:49:59" bu i want to display it in this format "الأربعاء, 9 مارس, 2016 10:33 ص" so how can i do this?
i mage showing the format in which i want iot

You can make use of NSDateFormatter and locale "ar_DZ", with a custom format specification to fit your needs: "EEEE, d, MMMM, yyyy HH:mm a".
// input date in given format (as string)
let inputDateAsString = "2016-03-09 10:33:59"
// initialize formatter and set input date format
var formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
// read input date string as NSDate instance
if let date = formatter.dateFromString(inputDateAsString) {
// set locale to "ar_DZ" and format as per your specifications
formatter.locale = NSLocale(localeIdentifier: "ar_DZ")
formatter.dateFormat = "EEEE, d, MMMM, yyyy HH:mm a"
let outputDate = formatter.stringFromDate(date)
print(outputDate) // الأربعاء, 9 مارس, 2016 10:33 ص
}
Note that the above uses the default gregorian calendar (in so not translating e.g. year 2016 to year 1437 (/1438 ~October 2016) in the islamic calendar).
(Edit addition regarding your comment below)
If you change localeIdentifier above from "ar_DZ" to "ar", also numeric values gets written in arabic characters:
print(outputDate) // الأربعاء, ٩ مارس, ٢٠١٦ ١٠:٣٣ ص
However, I don't know arabic, so I can't really say if your image above displays that, and I'm no longer certain what you're trying to achieve; possibly this is not it.

Check this
let morningOfChristmasComponents = NSDateComponents()
morningOfChristmasComponents.year = 2014
morningOfChristmasComponents.month = 12
morningOfChristmasComponents.day = 25
morningOfChristmasComponents.hour = 7
morningOfChristmasComponents.minute = 0
morningOfChristmasComponents.second = 0
let morningOfChristmas = NSCalendar.currentCalendar().dateFromComponents(morningOfChristmasComponents)!
/***** NSDateFormatter Part *****/
let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.timeStyle = .MediumStyle
formatter.locale = NSLocale(localeIdentifier: "ar")
let dateString = formatter.stringFromDate(morningOfChristmas)
txtMarks.text = dateString

For solving of youre issue try to use legend way with NSCalendar style:
var today = NSDate()
let islamic = NSCalendar(identifier:NSCalendarIdentifierIslamicCivil)!
let formatter = NSDateFormatter()
formatter.dateStyle = .LongStyle
formatter.timeStyle = .MediumStyle
formatter.calendar = islamic
formatter.locale = NSLocale(localeIdentifier: "ar_DZ")
formatter.stringFromDate(today) // "30 جمادى الأولى، 1437 هـ، 11:22:10 ص"
To configure proper style set yore's dateStyle and timeStyle also set up youre NSLocale.

Related

How to extract today, yesterday from Date() and make it localised like weekday and months?

I want to extract today, yesterday from date so it is localised automatically when I change language like which happens with weekdays and month.
Code for localised date that I am using:
let dateFormatter = Foundation.DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(identifier: "UTC")
let dateStr = dateFormatter.string(from: datePickerView.date)
let timeStr = "00:00:00.000"
let dateString = NSString(format:"%# %#",dateStr,timeStr)
let mainDF = Foundation.DateFormatter()
mainDF.dateFormat = "MMMM dd, yyyy hh:mm:ss.SSS"
mainDF.locale = Locale(identifier: "en_US_POSIX")
mainDF.timeZone = TimeZone(identifier: "UTC")
let date = mainDF.date(from: dateString as String)
How to achieve this?
DateFormatter has special flag for that: doesRelativeDateFormatting, which renders dates in relative format, using locale set for this formatter.
… If a date formatter uses relative date formatting, where possible it
replaces the date component of its output with a phrase—such as
“today” or “tomorrow”—that indicates a relative date. The available
phrases depend on the locale for the date formatter; whereas, for
dates in the future, English may only allow “tomorrow,” French may
allow “the day after the day after tomorrow,” …
Example:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.autoupdatingCurrent // Using system locale
dateFormatter.doesRelativeDateFormatting = true // Enabling relative date formatting
// other dataFormatter settings here, irrelevant for example
dateFormatter.timeStyle = .none
dateFormatter.dateStyle = .medium
let now = Date()
let dateString: String = dateFormatter.string(from: now)
print("dateString: \(dateString)") // Prints `dateString: <Today in current locale>`

Date from String with .medium Format

I have a date on a UITextField represented by its .medium formatter style (e.g. Mar 14, 2017), that I want to turn back into date.
I know that if I have the correct format, I can do so, but since I didn't use a literal string as the format, I have a little trouble with the conversion.
I am trying:
let formatter = DateFormatter()
formatter.dateFormat =
//[NEED FORMAT STRING HERE]
calendarPicker.startDate = formatter.date(from: t3.text!)
But I have found no way to turn the .medium style into a string to reformat the string into a date.
Can somebody provide some help?
I've used this formatter with style in Swift 3.0 and it works perfectly fine.
let formatter = DateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
//Specified date format
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
//Style can be medium, full and short as per the need.
formatter.dateStyle = .medium
let parsedDateString = formatter.date(from: stringDate)
Thank you.
Were you looking for dateStyle ?
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.medium
calendarPicker.startDate = formatter.date(from: t3.text!)

swift NSDate AM and Pm Format [duplicate]

This question already has answers here:
NSDateFormatter- stringFromDate not returning AM/PM
(6 answers)
Closed 5 years ago.
To convert date and time, I have used this:
let formatter = NSDateFormatter()
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
formatter.dateFormat = "M/dd/yyyy, h:mm"
formatter.AMSymbol = "AM"
formatter.PMSymbol = "PM"
let dateString = formatter.stringFromDate(model.courseDate)
print(dateString)
If the course date is "courseDate = "2017-01-09 01:00:00 +0000";"
but I am getting like this in print:
1/09/2017, 1:00 with no any AM or PM.
How can I achieve this?
Thanks.
according to the date formatter specs
You can retrieve a date in 24h format instead of requesting AM/PM by using
HH instead of hh
so the date "21/11/2016 01:30" (pm) would be "21/11/2016 13:30".
if you do need to print the format, though, you can print it using:
a
More date fomatting details here
As per Apple general date formatter you should use "M/dd/yyyy, hh:mm a" in your date format.
like : formatter.dateFormat = "M/dd/yyyy, hh:mm a"
Hope this will help you!
use following code may help full for you
//Call function
let dateTime = self.convertDateFormaterAamer(sendTimeStep, inputDateFormate: "dd-MM-yyyy HH:mm:ss", outputDateFormate: "dd-MMM-yyyy hh:mm a")
//Date function
func convertDateFormater(date: String,inputDateFormate: String , outputDateFormate: String) -> String
{
let dateFormatter = NSDateFormatter()
//dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = inputDateFormate
let date = dateFormatter.dateFromString(date)
//dateFormatter.dateFormat = "yyyy MMM EEEE HH:mm"
//dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = outputDateFormate
var timeStamp = ""
if (date != nil)
{
timeStamp = dateFormatter.stringFromDate(date!)
}
return timeStamp
}

Getting back a date from a string

im dealing with dates, and i'm having problems getting my date back from a string, i simplified my problem here:
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-YYYY"
formatter.stringFromDate(date)
formatter.dateFromString(formatter.stringFromDate(date))
And the output is:
"25-05-2015" (Which is fine)
"Dec 21, 2014, 12:00 AM" (???)
The problem there is that Y is for weekOfYear. You have to use "dd-MM-yyyy". Btw don't forget to set your date formatter locale to "en_US_POSIX" .
If you're working with fixed-format dates, you
should first set the locale of the date formatter to something
appropriate for your fixed format. In most cases the best locale to
choose is "en_US_POSIX", a locale that's specifically designed to
yield US English results regardless of both user and system
preferences. "en_US_POSIX" is also invariant in time (if the US, at
some point in the future, changes the way it formats dates, "en_US"
will change to reflect the new behaviour, but "en_US_POSIX" will not),
and between machines ("en_US_POSIX" works the same on iOS as it does
on OS X, and as it it does on other platforms).
You should use yyyy for the year, not YYYY (which has a different meaning)
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
let s = formatter.stringFromDate(date) // "25-05-2015"
let d = formatter.dateFromString(s) // "2015-05-24 22:00:00 UTC" (*)
(*) it's 22:00 because I'm in the +0200 timezone, so this result is effectively 2015-05-25 00:00:00 in my timezone
You should change your dateFormat to make it work , YYYY is not correct.
import Foundation
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-YYYY"
var firstDate = formatter.stringFromDate(date)
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSxxx"
var secondDate = formatter.dateFromString(formatter.stringFromDate(date))
println("\(firstDate)")
println("\(secondDate)")

How to display date with human language like "Today at xx:xx pm", "Yesterday at xx:xx am"?

I have a date "2014-07-02 20:57:38 +0000" and I want to format it as "Today at 8:57 pm".
I want that if a string is yesterday, then display it as "Yesterday at 9:00 am". If it is neither today or yesterday, just show the actually date like "27/6 at 7:53 pm".
I was able to get the time with format like "8:57 AM" with the code below.
var formatter : NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "h:mm a"
// message.createdAt is the date
let dateString = formatter.stringFromDate(message.createdAt)
println(dateString)
//output = 8:57 AM
However, when I use the following code, it returns a blank string.
var formatter : NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "h:mm a"
formatter.doesRelativeDateFormatting = true //<-- This doesn't work
let dateString = formatter.stringFromDate(message.createdAt)
println(dateString)
//output = (nothing, its a blank string)
How do I make this work and display "Today" or "Yesterday" in Swift?
The reason it's blank is that your date format only has time components. Combined with .doesRelativeDateFormatting that gives you the empty string. If you want that custom time format, I think you need separate formatters for the date and the time:
let now = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
dateFormatter.doesRelativeDateFormatting = true
let timeFormatter = NSDateFormatter()
timeFormatter.dateFormat = "h:mm a"
let time = "\(dateFormatter.stringFromDate(now)), \(timeFormatter.stringFromDate(now))"
println(time) // prints "Today, 5:10 PM"
With Swift 5.1, Apple Developer API Reference states about DateFormatter's dateFormat property:
You should only set this property when working with fixed format representations, as discussed in Working With Fixed Format Date Representations. For user-visible representations, you should use the dateStyle and timeStyle properties, or the setLocalizedDateFormatFromTemplate(_:) method if your desired format cannot be achieved using the predefined styles; both of these properties and this method provide a localized date representation appropriate for display to the user.
The following Playground sample code shows how to display your dates in the desired format using dateStyle, timeStyle and doesRelativeDateFormatting properties:
import Foundation
let now = Date() // 2019-08-09 12:25:12 +0000
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now)! // 2019-08-08 12:25:12 +0000
let aWeekAgo = Calendar.current.date(byAdding: .weekOfMonth, value: -1, to: now)! // 2019-08-02 12:25:12 +0000
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .short
dateFormatter.doesRelativeDateFormatting = true
let nowString = dateFormatter.string(from: now)
print(nowString) // prints: Today at 2:25 PM
let yesterdayString = dateFormatter.string(from: yesterday)
print(yesterdayString) // prints: Yesterday at 2:25 PM
let aWeekAgoString = dateFormatter.string(from: aWeekAgo)
print(aWeekAgoString) // prints: August 2, 2019 at 2:25 PM
Give this a try in Swift:
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
dateFormatter.doesRelativeDateFormatting = true
let date = Date()
let dateString = dateFormatter.string(from:date)

Resources