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]];
Related
In my application i am using one function to change date format to replace / with - and date format also.
When Device is set to 12 Hour, everything works fine. But when i set it to 24Hour it returns wrong value.
NSDate *newdate = [self convertDateSlashToDash:[obj valueForKey:#"TaskStartDateTime"]]);
//input date is : 6/6/2017 6:38:00 PM
-(NSDate *)convertDateSlashToDash:(NSString *)dateStr{
if ([dateStr isKindOfClass:[NSDate class]]) {
return (NSDate*)dateStr;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set the AM and PM symbols
[dateFormatter setAMSymbol:#"AM"];
[dateFormatter setPMSymbol:#"PM"];
//Specify only 1 M for month, 1 d for day and 1 h for hour
[dateFormatter setDateFormat:#"M/d/yyyy h:mm:ss a"];
// in ms 1469077819000 without ms 1469077819 for 7/21/2016 5:10:19 AM
NSTimeInterval ti = [[dateFormatter dateFromString:dateStr] timeIntervalSince1970];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:ti];
NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
// [formatter setLocale:[NSLocale currentLocale]];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss aa"];
NSString *dateString = [formatter stringFromDate:date];
NSDate *parsedDate = [formatter dateFromString:dateString];
return dateString;
}
//output of this is (24hrs): 1970-01-01 05:30:00AM
//output of this is (12hrs): 2017-06-06 06:38:00 PM
Why it is not working?
Please suggest.
Thank you.
Please add this line to your code to resolve that problem
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
Reade more: https://developer.apple.com/library/content/qa/qa1480/_index.html
The 12-hour and 24-hour are specified differently with a date formatter. Make sure that you are using the right format. I see you are using h for hour along with a at the end which configures the formatter to a 12-hour format. For a 24-hour format, you need H or HH depending on your needs. You can refer to this link to get a better understanding of different formats
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 want to apologize ahead of time for asking a repeat question, but none of the other solutions have worked for me yet. Every time I try to pass a date string to the dateFromString function I get nil. I haven't read anywhere that things have changed since the iOS 7 update, but I am current on updates if that makes a difference on whether or not this still works the same way.
This is the code I'm using to create the date from string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale systemLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:dateString];
return date;
I've set up my dateFormat based on all the solutions I've read to solve this problem, but none of these settings have solved by problem. The systemLocale is definitely set up for English so that should not be causing any issues.
This is the dateString I'm passing to dateFromString:
Wednesday, October 9, 2013 at 2:40:29 PM Pacific Daylight Time
Thanks for the help!
There are two issues here:
The format of date string the formatter is expecting (#"yyyy-MM-dd hh:mm:ss") is different from the format of the date string you're trying to parse (#"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz").
Setting the formatter's locale to [NSLocale systemLocale] is causing [dateFormat dateFromString:] to return nil. Set it to [NSLocate currentLocale].
The full code for the formatter should be:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:dateString];
Yet another way to get nil is if you use hh and your hours are on the 24 hr clock and > 12, in that case, you need HH (or H, for zero-padded).
That is:
Format: yyyy-MM-DD hh:mm:ss, string: "2016-03-01 13:42:17" will return nil
Format: yyyy-MM-DD HH:mm:ss, string: "2016-03-01 13:42:17" will return the date you expect.
Hat-tip to #neilco (see comments below his answer) for this. If you like this answer, please up-vote his, too.
According to NSDateFormatter documentation :
When working with fixed format dates, such as RFC 3339, you set the
dateFormat
property to specify a format string.
If your date format is 2017-06-16T17:18:59.082083Z then dateFormat property should look like this yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ.
Swift 3
let dateFormatter = DateFormatter()
let date = "2017-06-16T17:18:59.082083Z"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
let result = dateFormatter.date(from: date) // 2017-06-16 17:18:59 +0000
Your date format doesn't match the string that you're passing, your dateString should be in this formate as per your [dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
2013-10-09 02:40:29
nil means dateFormat object was unable to parse your string.
In case anybody is stuck on the same hilarious edge case as me:
"2020-03-08T02:00:00" will return nil as long as you're in a locale that follows Daylight Savings Time, because that hour is skipped and simply doesn't exist.
You're trying to use dateFromString but the Format you have passed to your Formatter is different of what you're using in dateString.
Try to use this config in your dateFormat: E',' M d',' yyyy 'at' hh:mm:ss aa z
Don't forget to escape "yyyy/MM/dd' 'HH:mm:ss" space symbols
Team,
I am comparing the date which is formed from string using NSDateFormatter with the iOS system date. The below statement returns true when the system date time settings is set with 24-Hour Time ON, but the same code returns false when 24-Hour Time OFF.
Problematic Code:
if ([(NSDate*)[NSDate date] compare:currDate] == NSOrderedAscending) {
// -- Code -- This is executed only when the 4-Hour Time ON
}
I am confused. The string using which I am getting the date is in 24 hours format. Is this a problem? Or anything else?
Date Formatting Code:
-(NSDate *)getDateFromString:(NSString *)dateString{
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:#"dd MMM yyyy hh:mm:ss"];
[fmt setTimeZone:[NSTimeZone systemTimeZone]];
[fmt setFormatterBehavior:NSDateFormatterBehaviorDefault];
return [fmt dateFromString:dateString];
}
See Apple's Technical Q&A 1480.
You need to set the date formatter's locale to the special locale of en_US_POSIX. You also need to specify a 24-hour hour format - HH, not hh.
-(NSDate *)getDateFromString:(NSString *)dateString{
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setLocale:posix];
[fmt setDateFormat:#"dd MMM yyyy HH:mm:ss"];
return [fmt dateFromString:dateString];
}
23 May 2013 15:37:00 is a 24 hour format string.So the correct date is obtained using the 24 hour format date formatter .Thats it
So use
[fmt setDateFormat:#"dd MMM yyyy HH:mm:ss"];