Dateformatter issue in swift - ios

My API sending me date string in UTC like 2/20/2016 11:45 AM.
For displaying date in my timezone, I am converting date from UTC to Local time zone like below code
let sourceDateString = "12/20/2016 11:45 AM"
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy hh:mm a"
formatter.timeZone = TimeZone(identifier: "UTC")
let date = formatter.date(from: sourceDateString)
print(date)
formatter.timeZone = NSTimeZone.local
formatter.dateFormat = "MMM dd,yyyy hh:mm a"
let strDate = formatter.string(from: date!)
print(strDate)
Above code is working fine if my device time format is in 12 HR but if I am using 24 Hr date from my device then above code is not working. Event it's not able to convert UTC string to date.
Please check above code in the device, it's working fine in simulator.

With help of #Martin R, Following code worked for me.
let sourceDateString = "12/20/2016 11:45 AM"
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy hh:mm a"
formatter.timeZone = TimeZone(identifier: "UTC")
formatter.locale = Locale(identifier: "en_US_POSIX")
let date = formatter.date(from: sourceDateString)
print(date)
formatter.timeZone = NSTimeZone.local
formatter.dateFormat = "MMM dd,yyyy hh:mm a"
let strDate = formatter.string(from: date!)
print(strDate)
I forgot to add "POSIX".

Code written in Swift 3.0
You need to give local identifier on date formatter.
Here is the following working and tested code.
let sourceDateString = "12/22/2016 09:19 AM"
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy hh:mm a"
formatter.locale = Locale(identifier: "en-US")
formatter.timeZone = TimeZone(identifier: "UTC")
let date = formatter.date(from: sourceDateString)
print("\(date)")
formatter.timeZone = NSTimeZone.local
formatter.dateFormat = "MMM dd,yyyy hh:mm a"
let strDate = formatter.string(from: date!)
print(strDate)
Please let me know if it works.

Quick fix for this is to convert 12hr to 24 hr format.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateformatter.dateFormat = #"hh:mm a";
NSDate *date = [dateformatter dateFromString:departTime];
dateformatter.dateFormat = #"HH:mm";
NSString *time24 = [dateformatter stringFromDate:date];
departTimelbl.text=time24;
here you can replace
formatter.dateFormat = "MM/dd/yyyy hh:mm a" with formatter.dateFormat = "MM/dd/yyyy HH:mm

Related

Swift DateFormatter returning wrong date

I have a issue with getting the correct date in the following case
let dateString = "May 2, 2018 at 3:31 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy 'at' HH:mm a"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: dateString)!
I am getting "May 2, 2018 at 8:31 AM" as the date, which is wrong
When the same date is used with a different dateformatting string i get the correct date
let dateFormatter3 = DateFormatter()
dateFormatter3.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter3.locale = Locale(identifier: "en_US_POSIX")
dateFormatter3.timeZone = TimeZone(identifier: "UTC")
let date3 = dateFormatter3.date(from: "2018-05-02T15:31:00")!
Date returned is "May 2, 2018 at 11:31 AM" which is correct.
I like to know what i am doing wrong in the first code block? Or is this a bug in the date formatter class ?
when hour is in am/pm(1~12) use hh, to hour in day(0~23) use HH.
This helps me format my dates:
I hope this would work for you:
let dateString = "May 2, 2018 at 3:31 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy 'at' hh:mm a"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: dateString)
print(date!)
Output:-

Date Formatter in swift

i want to get this type of formate -> April 20, 2018 at 9:02:00 PM UTC+5:30
for firestore timestamp. i already try this code but do not get proper formate as above.
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM d yyyy hh:mm:ss a Z"
formatter.timeZone = NSTimeZone(name: "IST") as TimeZone?
let result = formatter.string(from: date)
print(result)
already use this formatter formatter.dateFormat = "MMMM d yyyy'T'hh:mm:ss a Z"
Thanks
You can use like this:
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss a 'UTC'Z" //If you dont want static "UTC" you can go for ZZZZ instead of 'UTC'Z.
formatter.timeZone = TimeZone(abbreviation: "IST")
let result1 = formatter.string(from: date)
print(result1)
Note: If you do not want the UTC to be static in the string, you can also use the format "MMMM dd, yyyy 'at' hh:mm:ss a ZZZZ"
Thank you Larme For the format improvement
Output:
do like
let formatter = DateFormatter()
//If you want it in your specific format you can simply add 'UTC'
formatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss a 'UTC'Z"
formatter.timeZone = NSTimeZone(name: "IST") as TimeZone?
let result = formatter.string(from: Date())
print(result)
Note: if you want to set the currentTimezone for your string then use the format "MMMM dd, yyyy 'at' hh:mm:ss a ZZZZ"
you get the output as
Try this if you don't want leading zero in hour, when hour has single digit. Also you don't need to typecast NSTimeZone into TimeZone. You can directly use TimeZone.
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy 'at' h:mm:ss a 'UTC'Z"
formatter.timeZone = TimeZone(abbreviation: "IST")
let result = formatter.string(from: date)
print(result)
April 23, 2018 at 3:09:01 PM UTC+0530

Converting a full date into just hours and minutes - Swift

I'm trying to do something very simple here and failing miserably. I just want to convert a string with a complete date into just the hours and minutes, can anyone see where I am going wrong? The following code prints nil
let dateString2 = "2018-03-11 20:43:05 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "en_GB")
let dateObj = dateFormatter.date(from: dateString2)
dateFormatter.dateFormat = "hh:mm"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")
Your dateFormat String is flawed. hh is the 12hour time format, when you clearly receive a 24h time format, for which you need to use HH. Even though zzzz works, +0000 should actually be represented by z. When working with fixed time formats in most cases you should also set the locale to en_US_POSIX.
let dateString2 = "2018-03-11 20:43:05 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss z"
dateFormatter.locale = Locale.init(identifier: "en_US_POSIX")
let dateObj = dateFormatter.date(from: dateString2)
dateFormatter.dateFormat = "HH:mm"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")
Output:
20:43
You need HH (24-hour) for the hour, not hh (12-hour). You also need to use the special locale of en_US_POSIX, not en_GB. You should also use Z for the timezone, not +zzzz.
let dateString2 = "2018-03-11 20:43:05 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let dateObj = dateFormatter.date(from: dateString2) {
dateFormatter.dateFormat = "HH:mm" // or "hh:mm a"
print("Dateobj: \(dateFormatter.string(from: dateObj))")
}

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
}

swift - Converting String to NSDate [duplicate]

This question already has answers here:
NSDateFormatter return incorrect date from string
(3 answers)
Closed 7 years ago.
I'm trying to convert a Date and Time String to NSDate with Swift, But the problem is The time is not converting perfectly, Even i set my Timezone correctly, Here's my code:
var dateStr = "2015-10-16 08:00 AM"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 28800)
var date = dateFormatter.dateFromString(dateStr)
print(date!)
And it prints out like this: 2015-10-16 00:00:00 +0000 What do you think seems to be the problem? Thanks!
Nothing wrong here. You should convert date to String before print it.
let dateStr = "2015-10-16 08:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 28800)
let date = dateFormatter.dateFromString(dateStr)
print(dateFormatter.stringFromDate(date!))
or as in Swift 3
let dateStr = "2015-10-16 08:00 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 28800)
let date = dateFormatter.date(from: dateString)
print(dateFormatter.string(from: date))
The same result: 2015-10-16 08:00 AM

Resources