Right dateFormat for ISO 8601 date - ios

I have stacked with dateFormat of 2017-12-13T10:30:00.000-06:00 for NSDateFormatter. I'd tried different formats but still getting nil from [formatter dateFromString:exampleDate]. The most possible format I had picked up on nsdateformatter was #"yyyy-MM-dd'T'HH:mm:ss.SSSZ" but TimeZone not applying correctly and I got nil from NSDateFormatter.
I also try to use:
NSISO8601DateFormatter *dateFormatter = [[NSISO8601DateFormatter alloc] init];
[dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
and also got nil.

You can use NSISO8601DateFormatter with options .withFractionalSeconds (iOS 11.0+) and .withInternetDateTime
NSISO8601DateFormatter *dateFormatter = [[NSISO8601DateFormatter alloc] init];
dateFormatter.formatOptions = NSISO8601DateFormatWithFractionalSeconds | NSISO8601DateFormatWithInternetDateTime;
NSDate *date = [dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
NSLog(#"%#", date);
For a standard NSDateFormatter the date format is correct
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSDate *date = [dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
NSLog(#"%#", date);

Related

iOS DateFormat with milliseconds and timezone

I'm trying to format a date in objective-c for an ios app in this format:
2017-02-15T16:32:59.9725843+11:00
with no luck. I've tried:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
formatter.dateFormat = #"yyyy-MM-dd'T'hh:mm:ss.SSSSSSSZZZZZ";
and
formatter.dateFormat = #"yyyy-MM-dd'T'hh:mm:ss.fffffffZZZZZ";
As well as a bunch of other formats for the milliseconds and timezone (I've varied the number of Ss and Zs with no luck. All I get is a nil NSString.
Any help would be appreciated.
Your your String Time format is 24 hour, so use HH:mm:ss. in this place hh:mm:ss.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ";
NSDate *getDate = [formatter dateFromString:#"2017-02-15T16:32:59.9725843+11:00"];
NSLog(#"getDate == %#",getDate);
output:
It's .SSS, for millisecond check also this Unicode Locale Data Markup Language spec
Try this code for milliseconds :
"yyyy-MM-dd'T'HH:mm:ss.SSS"
You can also check nsdateformatter and DataFormatting
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setTimeZone:[NSTimeZone localTimeZone]];
dateformatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ";
NSString *currentDate = [dateformatter stringFromDate:[NSDate date]];
NSLog(#"current Date :%# ",currentDate);
Output:
current Date :2017-02-15T11:35:11.7790000+05:30

How NSDateFormatter works?

I have created a below method in which i can send dateString, InputFormat & OutputFormat aswell as below.
- (NSString *)InstanceAwesomeFormatterunformattedDate:(NSString*)unformattedDate inputFormat:(NSString*)inputFormat outPutFormat:(NSString*)outPutFormat{
//unformattedDate is 13 july 1989
//inputFormat is #"dd MMMM yyyy"
//outPutFormat is #"dd"
NSDateFormatter *myDateFormattr = [[NSDateFormatter alloc] init];
[myDateFormattr setDateFormat:inputFormat];
NSDate *date = [myDateFormattr dateFromString:unformattedDate];
//Why date is 1990-07-12 18:30:00 +0000
[myDateFormattr setDateFormat:outPutFormat];
NSString *FinalExpiryDateForUserProfile = [myDateFormattr stringFromDate:date];
// FinalExpiryDateForUserProfile is 13 which is correct
return FinalExpiryDateForUserProfile;
}
I am trying to understand why date prints 12 instead of 13.
Any ideas
You need to use locale
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd"];
NSLocale *enLocale = [NSLocale localeWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:enLocale];
Problem is with time zone and you can set timezone like this:
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
You can add any timezone instead of GMT as per your requirement. Setting NSLocale is not really mandatary if you set proper timezone as per your settings.

How to format date using NSDateFormatter iOS?

I tried to use the NSDateFormatter to format a date that selected from a UIDatepicker. But the formatted result giving a nil.
-(NSString *)formatDateWithString:(NSString *)date{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MMM-dd HH:mm:ss";
NSDate *formattedDate = [dateFormatter dateFromString:date];
return [dateFormatter stringFromDate:formattedDate];
}
date input is 2016-04-22 16:30:36 +0000(after selected from UIDatePicker). How may I fix this?
As mentioned in the comment you can get the NSDate directly from the UIDatePicker and pass that date directly in the formatter
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:datePicker.date];//pass the date you get from UIDatePicker
If you want to convert the same way you have mentioned in the question you can try like this:
-(NSString *)formatDateWithString:(NSString *)date{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
NSDate * convrtedDate = [formatter dateFromString:date];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:convrtedDate];
return dateString;
}

Converting a NSString to NSDate

I am trying to convert a NSString into a NSDate as shown below.
The value of NSString *startTime is 2015-06-23T01:37:53Z,
but the value of NSDate *startTimeDate is nil. What is wrong with the code ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
check your date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
change into
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
Swift
check your date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
change into
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
There are at least 2 issues with your date format:
.SSS is used to read milliseconds but variable startTime doesn't contains milliseconds value;
Z represent GMT time zone and must not be escaped in dateFormat string.
Let's try to fix this ussues:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
I recommend to use this NSDateFormatter date formatting table. It's very comprehensive and helpful.
The startTime you've specified doesn't have any milliseconds, so you want to use a dateFormat of:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
If you want to support both styles of XML date strings then I recommend creating two NSDateFormatter instances for both date formats, and try the other if you get nil from the first.
Its simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the NSDateFormatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];

iOS Convert NSString to NSDate alway nil

I'm trying to convert string to NSdate. The string date is "12/27/2014 07:42:00 AM +0000"
Here is my code:
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yyyy HH:mm:ss a";
date = [ dateFormatter dateFromString:dateString];
The date value is always nil. Any of you knows what canI be doing wrong?
I'll really appreaciate your help.
try
dateFormatter.dateFormat = #"MM/dd/yyyy hh:mm:ss a Z";
as HH is 24-hour and conflicts a. Z stands for the timezone offset.
have a look on the specifiers: http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yyyy hh:mm:ss a Z";
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
// see https://developer.apple.com/library/ios/qa/qa1480/_index.html
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *str =#"12/27/2014 07:42:00 AM +0000";
NSDateFormatter *sdateFormatter = [[NSDateFormatter alloc] init];
sdateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[sdateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a Z'"];
NSDate *sdate = [sdateFormatter dateFromString:str];
NSLog(#"%#",[sdateFormatter stringFromDate:sdate]);

Resources