NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
[dateFormat setTimeZone:gmt];
[dateFormat setDateFormat:#"h:mm a"];
return [dateFormat dateFromString:text];
where text is 9:16 AM used to return a date starting from 1970 in iOS6 its returning a date like this
2000-01-01 09:16:00 +0000
is this a bug? anyway to switch to old vehaviour
on solution is to simply add "1970" to time text and then add "yyyy" to format but that seems kludgy
They've definitely changed something in iOS 6 with the date parsing. I already submitted a bug to Apple about it. There's noting in the release notes about any NSDateFormatter changes. Also see my similar bug around this parsing I added today.
Related
As before XCode 9 I had no issue regarding DateFormatter.
With the update I have a issue I do not seem to be able to bypass.
I am busy converting dates from NSString to NSDate, however my code seems to not be able to initialize the NSDate.
My Code:
NSString *testdate = #"Sep 20 2017 1:46PM"; // this is how my date looks
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd yyyy hh:mma"];
NSDate *thisDate = [df dateFromString: testdate]; // this returns nil
I have tried manipulating the string in various ways, and only after I turned the function I found the dateformat issue to have appeared.
Has anyone found this issue and a possible solution?
Edit:
The only device I have this on is a iPhone 7 with iOS 11 (two different devices was tested and iPhone 7 with iOS 10 is also working correctly). I have an iPhone 6 plus as well as an iPhone 7 Plus which are both on iOS 11, and both these are working... with the time format
When I convert the [NSDate date] to string with the same date formatter my result is: M09 20 2017 1:46PM... - this was a date format issue it seems(seems to have happened when I changed the timeformat to test) and is now working however is not the problem to my issue
After closer investigation it seems like if I have setup a DateFormatter I also need to add a Locale to get it to work... The dates I got from a server so I can't just change it, due to being used on a website as well.
I received the date from the server as AM/PM and the Locale solved the issue
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd yyyy hh:mma"];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
This solved the issue that I had.
I am testing different time zones UTC offsets in application. And finally this code is properly working almost with all timezones. But i have an issue with Caracas.
Code that shows UTC offset.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
//This NSDateFormatter will return timezone in format "UTC+XX:XX"
[dateFormatter setDateFormat:#"'UTC'xxxxx"];
NSString *formattedTimeZone = [dateFormatter stringFromDate:[NSDate date]];
return formattedTimeZone;
In Ukraine i receive UTC+03:00 and it is correct. In Caracas i receive UTC-04:00 but real offset is UTC-04:30.
Question is why i am missing -30 minutes in Caracas?
This is not a programming problem, Caracas(Venezuela) timezone has changed recently.
UTC-04:00 is correct right now.
Presidents of Venezuela had changed this a couple of times:
UTC-04:30 was used since 2007.
It was recently changed again to UTC-04:00.
http://www.bloomberg.com/news/articles/2016-04-14/maduro-orders-time-zone-change-to-battle-venezuela-power-crisis
I get a date 2012-11-21 03:57:39-04 and I can't get an NSDate from it. I use the yyyy-MM-dd HH:mm:sszz format. I guess I'm stuck with the time zone part. I've tried different kinds of 'Z' and 'z' types but still can't get it.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:sszz"]; // "2012-11-21 03:57:39-04"
The z-Codes doesn't fit, because they always requires minutes. Try a single X or x instead.
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
X 1 -08
+0530
Z The ISO8601 basic format with hours field and optional minutes field. The ISO8601 UTC indicator "Z" is used when local time offset is 0. (The same as x, plus "Z".)
Just append #"00" and you should be fine
NSString *dateStr = [#"2012-11-21 03:57:39-04" stringByAppendingString:#"00"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *en_US_POSIXLocale =
[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormat setLocale:en_US_POSIXLocale];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ssZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
Try this format:
[dateFormatter setDateFormat:#"yyyy-MM-dd' 'HH:mm:sszzz"];
I found the error. The format wasn't the problem because the above format #"yyyy-MM-dd HH:mm:sszz" did actually work. Sorry for disturbing.
answer posted by the OP Iryna Tsimokhautsava
I found the error. The format wasn't the problem because the above
format #"yyyy-MM-dd HH:mm:sszz" did actually work. Sorry for
disturbing.
I'm having a problem. I get incoming time strings in 12-hour format, and I'm turning them into NSDate objects. When the iPhone is in 12 hour format, no problem. But when it's in 24 Hour format, things go wrong. Here's some sample code to demonstrate:
NSString *theTime = #"3:19 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"]; // "3:19 PM"
NSDate *date = [formatter dateFromString:theTime];
NSString *theString = [formatter stringFromDate:date];
In 24 hour mode, date is 1970-01-01 03:19:00, and theString is "3:19" - WRONG
In 12 hour mode, date is 1970-01-01 15:19:00, and theString is "3:19 PM" - RIGHT
So... question 1: why is the device's 24 hour setting overriding my date formatter setting?
and more importantly, question 2: How do I get a proper conversion from 12 hour time to 24 hour time?
I already have code to detect if the phone is in 24 hour mode, but other than digging around in the string and swapping the 3 with a 15, there doesn't seem to be a clean way to do this.
Not sure if you still need it, but I've had a similar problem which got solved by setting the locale for the date formatter. That is, if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX.
The reason for this behaviour is Locale, set the correct Locale
NSString *strAgendaDate = #"01/17/2012 12:00 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[dateFormatter setDateFormat:AgendaDateFormatForMeeting];
NSDate *meetingDate = [dateFormatter dateFromString:aStrDate];
[dateFormatter setDateFormat:AgendaDateRepresentation];
strAgendaDate = [dateFormatter stringFromDate:meetingDate];
It works for both 24-hour and 12 hour format
I believe the #"h:mm a" should be #"HH:mm a".
If you use the pre-build dateformatter in cocoa, everything will be taken care of for you.
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
NSDateFormatterShortStyle and NSDateFormatterNoStyle comes in different varieties.
Using those will make sure you respect the settings the user has selected for dates and times.
The 12-14 hour clock conversion is taken care of by the SDK, if you have a model or some value object for storing your dates try to keep them as NSDate. This way you can format them only when you need to display them. Saving dates as strings could open a world of trouble when you maybe parse them from xml where the GMT is specified separately or try to add and subtract NSTimeIntervals.
I changed from #"hh:mm:ss" to #"HH:mm:ss" and time style was changed from "1:03 PM" to "13:03".
Hope this will help you.
Okay, I left a comment, but it squished all the code together, so I'll have to "answer" my question with a comment:
Thanks. I gave it a whirl with this code:
NSString *theTime = #"3:19 PM";
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
NSDate *date = [timeFormatter dateFromString:theTime];
NSString *theString = [timeFormatter stringFromDate:date];
And date comes up nil. I ran into this earlier when I tried this route, and it's not working. Very frustrating.
I have a problem on parsing a date in IOS. Here is the date I got:
2013-12-22T20:30:58.020Z
and I cannot parse this date with the following code block:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss.SSSZ"];
entity.updatedAt = [dateFormatter dateFromString:dateString];
What is wrong with this formatting?
[EDIT]
When I changed my hour format to HH, it worked in simulator. However hh works fine while debugging in device. Do you have any opinions about this inconsistency?
You're using hh, which is the 12-hour clock - but you've provided a value of 20. You want HH, which is the 24-hour clock:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
RFC Date Time
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];