DateFormatter sets AM/PM by default - ios

I'm trying to convert the current time and date to a timestamp format. Since the service is receiving this format for timestamps:
2018-26-11T11:38:00Z
I decided to use a format like this:
yyyy-M-dd'T'H:mm:ss'Z'
But, when I use a date formatter to convert it, I'm getting an unwanted AM/PM tag at the end by default:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-M-dd'T'H:mm:ss'Z'"
let currentTimeAndDate = Date()
let timeStamp = dateFormatter.string(from: currentTimeAndDate)
// Prints "2018-12-05T12:58:38 PMZ"
How can I remove that AM/PM by default at the end of the string?

Set the locale to fixed en_US_POSIX"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
Or use the dedicated ISO8601 formatter
let dateFormatter = ISO8601DateFormatter()
let currentTimeAndDate = Date()
let timeStamp = dateFormatter.string(from: currentTimeAndDate)

Use yyyy-MM-dd'T'HH:mm:ssX for the format.
Set the formatter's timeZone to TimeZone(secondsFromGMT: 0).
Set the locale to the special "en_US_POSIX" locale: Locale(identifier: "en_US_POSIX")
Updated code:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssX"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let currentTimeAndDate = Date()
let timeStamp = dateFormatter.string(from: currentTimeAndDate)
Or avoid all of that and use ISO8601DateFormatter.

Related

Ios Swift Date Convert from TimeZone

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
let nowstring = dateFormatter.string(from: Date())
let nowdate = dateFormatter.date(from: nowstring)
print(nowstring)
print(nowdate)
print this 2018-07-24T15:06:26
Optional(2018-07-24 06:06:26 +0000)
i want nowdate value 2018-07-24T15:06:26
but String to Date without timezone
I have to use to timeIntervalSince so nowdate is must have timezone
let today = NSDate()
print("\(today)")
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let nowDatestring = formatter.string(from: today)
formatter.timeZone = TimeZone(abbreviation: "KST")
let kstDate = formatter.date(from: nowDatestring)
print("\(kstDate!)")
formatter.timeZone = TimeZone(abbreviation: "PST")
let pstDate = formatter.date(from: nowDatestring)
print("\(pstDate!)")
formatter.timeZone = TimeZone(abbreviation: "IST")
let istDate = formatter.date(from: nowDatestring)
print("\(istDate!)")
Date is a specific point in time, which is independent of time zone. If you print a date value it will be always independent of any calendar or time zone. It will give same value for you(KST) and me(IST). You can specify your time zone when you format it using DateFormatter.

Time-zone Swift

Can somebody recommend how to get a current time/date in a specific timezone ?
This is what I got so far:
let date = NSDate()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let str = dateFormatter.string(from: date as Date)
dateFormatter.timeZone = NSTimeZone(name: "UTC") as! TimeZone
print(str)
It gives me my current time, I'm not sure how to change timezone correctly so it would print time in a specified time-zone...

Issue in converting date format

I have assigned the value of date to a dictionary like so...
userDetailsDictionary["birth_date"] = "\(birthDateVar)"
Now this gives the date in dd-mm-yyyy. But I want the date in yyyy-mm-dd. For that I tried something like this...
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let bday = dateFormatter.date(from: birthDateVar)
dateFormatter.dateFormat = "yyyy-MM-dd"
let bdayString = dateFormatter.string(from: bday!)
Though birthDateVar has the value in it, while I'm trying to convert it, it is showing as nil and so bday is becoming nil. Confused as to what the reason is...
change your date format from
dateFormatter.dateFormat = "MM-dd-yyyy"
to
dateFormatter.dateFormat = "dd-MM-yyyy"
Updated answer
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
let bday = dateFormatter.date(from: birthDateVar)
dateFormatter.dateFormat = "yyyy-MM-dd"
let bdayString = dateFormatter.string(from: bday!)

How to convert "2017-07-11T06:52:15.948Z" in to like "JUL, 7 2017" in swift

I'm trying to convert string to date, and again date to string.
I tried below, but it gives me a nil value error
let string = "2017-07-11T06:52:15.948Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
then I tried to use only the date without time by seperating the exact string like below
let string = "2017-07-11T06:52:15.948Z"
let dateonly = (string.components(separatedBy: "T"))[0]
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-mm-dd"
dateformatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
let date = dateformatter.date(from: dateonly!)
print(date ?? "no date ")
dateformatter.dateFormat = "MMM d, yyyy" //Your New Date format as per requirement change it own
let newDate = dateformatter.string(from: date!) //pass Date here
print(newDate)
this works fine. but show me the wrong month
hope your help with this
for reference purpose I taken the answer from here for append the milliseconds SSS
just change the date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
to
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
for full code
let string = "2017-07-11T06:52:15.948Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "MMM d, yyyy" ; //"dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale --> but no need here
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
output

Put the time in French

I try to format the time in French in my iOS app written in Swift 3
Here the data string
2000-01-01T14:00:00.000Z
Here method swift 3
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = "hh:mm"
return dateFormatter.string(from: timeStart!);
}
On my application gives its 2:00
And I want to display 2:00 p.m.
I used it: dateFormatter.locale = Locale (identifier: "fr_FR")
but its not working there have another manipulation do?
You need to fix both date formats:
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.string(from: timeStart!);
}
Better yet, don't use dateFormat to convert the Date to a String. Instead, set the timeStyle so it's formatted properly without the need to force a specific format.
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = nil
dateFormatter.timeStyle = .medium
dateFormatter.dateStyle = .none
return dateFormatter.string(from: timeStart!);
}
In either case, only set the locale if you want all users to see the time formatted in that specific locale (and that would be rare).

Resources