I tried executing the following code to get a date from an NSString:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"GMT"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssZ"];
NSDate * date = [formatter dateFromString:#"2015-01-18T09:33:49.699-0600"];
NSLog(#"%#",date);
Why am I receiving nil value?
The format string should be
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
Note the upper case .SSS.
And for the locale, you should specify #"en_US_POSIX" as advised in Technical Note TN1480. This will ensure that you can successfully parse this RFC 3339/ISO 8601 formatted date, regardless of the user's localization settings.
Related
I am trying to set the date to PayPal pre-approval key in the following way:
#"2015-04-27T10:45:52Z", #"startingDate",
This date works, however I don't know how to reproduce it in code terms. I tried doing:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"]; // Date formater
NSString *date = [dateformate stringFromDate:[NSDate date]];
but this doesn't work. What is the Z at the end of the date?
First, your date format is not correct. Second, for consistent results, you should always hard-code the en_US_POSIX locale (the date formatter defaults to the user's locale):
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
Alternatively, I've had positive experience with iso-8601-date-formatter. ISO 8601 is a surprisingly complex standard with lots of edge cases, and this library seems to be able to cope with most of them.
The Z stands for Zulu (i.e. UTC/GMT). If you want to generate a date string in that format (GMT with Z qualifier), please refer to Apple's Technical Q&A #1480, which reminds us to specify both locale and timeZone:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
Or in macOS 10.12 and iOS 10, you can do:
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
And then you can do:
NSString *dateString = [formatter stringFromDate:[NSDate date]];
I have these following snippet.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat : #"yyyy/MM/dd"];
NSString *timeStr = #"2015/05/16";
NSDate *timeDate = [formatter dateFromString:timeStr];
But when I print timeDate in console,the output is werid.The year becomes 4003.
4003-05-16 16:00:00 +0000
I test it in iPad 8.3(12F69)(not simulator).System time zone is Beijing.
Any help is appreciated.
The issue is the locale of the formatter. If you want to use gregorian calendar regardless of the device settings, you generally should set locale to en_US_POSIX.
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
See Apple Technical Q&A #1480. This is geared towards RFC 3339/ISO 8601 dates, but it really applies anywhere you're trying to use standard calendar for converting date string for internal purposes (i.e. as opposed to those dates that are presented to the end user in the UI, which generally should use the device's locale).
Try this... Just need to add last line while printing output.
NSString *timeStr = #"2015/05/16";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd";
NSDate *Date = [formatter dateFromString:timeStr];
NSLog(#"Date =%#", [formatter stringFromDate:Date]);
I've already searched StackOverflow.com for an answer to this question, still without any success. Already looked here:
iOS - Converting time and date to user time zone
Date Format - time zone
Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?
IOS how to set date format
Nothing of those worked.
So I have this NSString date format: 2014-05-07T10:28:52.000Z trying to convert it to NSDate, this is what I'm using:
+ (NSDate*)stringToDate:(NSString*)string format:(NSString*)format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
return [dateFormatter dateFromString:string];
}
This is how I'm using it:
NSDate *date = [AppUtils stringToDate:youtube.postDate format:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
I've also tried those formats:
yyyy-MM-dd'T'HH:mm:ss.ZZZ
yyyy-MM-dd'T'HH:mm.ss.ZZZ
yyyy-MM-dd'T'HH:mm.ssZZZ
How could I convert it to NSDate successfully?
Thanks in advance!
If the date format is fixed, what I do is below.
Replace T by space
Replace Z by blank
And then do formatting...
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"T" withString:#" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"Z" withString:#""];
Now do the formatting using
yyyy-MM-dd HH:mm:ss
Edit 1
If you want to work with your case, use below
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Edit 2
I tried this and it is working.
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"T" withString:#" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"Z" withString:#""];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(#"ddddd====%#", [dateFormatter dateFromString:dateReceivedInString]);
Edit 3
To make working with your case use below.
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(#"ddddd====%#", [dateFormatter dateFromString:dateReceivedInString]);
Here are 2 methods that I use to convert RFC3339 Date string to NSDate,
And also Method for converting NSDate to RFC3339 Date string
Method for converting RFC3339 Date string to NSDate
+ (NSDate *)dateForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
{
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Convert the RFC 3339 date time string to an NSDate.
return [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];}
Method for converting NSDate to RFC3339 Date string
+ (NSString *)RFC3339DateTimeFromDate:(NSDate *)aDate
{
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
return [rfc3339DateFormatter stringFromDate:aDate];
}
I would like to format a date to display in a uitableview custom cell
The data is passed into the app from a CMS - it is provided as a string in the following format and stored in a date type variable-
2014-04-15 10:10:45 +0000
Our app will initially be UK based - so I need to convert the format into DD/MM/YYYY format.
I tried the following code to parse my date (dateadded which is of type date).
NSDateFormatter *formatter = [NSDateFormatter new];
cellRecP.artDate.text = [formatter stringFromDate:resItem.dateadded];
but this just returns null - i guess the date format provided above is not anything that stringfromdate understands - is there any other way to format date?
Use an 'NSDateFormatter'
NSDateFormatter* newFormatter = [[[NSDateFormatter alloc] init] autorelease];
with this format to parse the string
[newFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss zzz"];
NSDate* aDate = [newFormatter dateFromString:dateString];
and you should get a valid 'NSDate' object
Try This and also Checkout this All Formate It is really good and helpful.
NSString *yourString = #"2014-04-15 10:10:45 +0000";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];//Set Your Timezone
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];//You have to set this formate.
NSDate *dd = [df dateFromString:yourString];//This will convert into date;
//Now you can set your formate.
[df setDateFormat:#"dd/MM/yyyy"];
NSString *str = [df stringFromDate:dd];
You need an NSDateFormatter and a proper unicode parse string. NSDateFormatter by default automatically checks the device locale setting the correct output.
This is an example from some code of mine:
NSDateFormatter *dateWriter = [[NSDateFormatter alloc] init];
dateWriter.dateFormat = #"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'ZZZ";
dateWriter.dateStyle = NSDateFormatterMediumStyle;
dateWriter.timeStyle = NSDateFormatterMediumStyle;
Pay attention that date formatters are pretty expensive to create.
The following code will set date to nil.
NSString *dateString = #"2014-04-27T04:20:07.000-04:00";
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
NSDate *date = [formatter dateFromString:dateString];
What am I doing wrong?
I've tried many other different variations for UTC_FORMAT, but counldn't seem to get it. I'm also a little bit confused as to when and where the single quotes go. After playing with this for a while, I'm assuming it can goes around characters that shouldn't be interpreted by the formatter, but that's a separate thing.
Related Links That Couldn't Help Me:
Apple Docs: Data Formatting Guid
SO: Why is NSDateFormatter returning nil?
Formats That I've Tried:
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSString *UTC_FORMAT = #"yyyy-MM-dd'T'HH:mm:ss-Z";
Your date looks like a quite standard JSON date format in RFC3339 format. However, there are several possibilities how these dates can be formatted. In this case, your date string contains milliseconds. Your date format doesn't, so this cannot work. The following code will check for dates without fractional seconds first, then for dates with fractional seconds. Furthermore, you are looking for a literal character Z instead of a timezone.
The "X5" is documented at
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
and converts time zones in quite a flexible way, including the colon in the middle. .SSSSSS will convert fractional parts of seconds up to microseconds. Should you be given nanoseconds change it to nine S characters.
And I forgot the locale information...
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ssX5";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
gmtTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setLocale:enUSPOSIXLocale];
[formatter setTimeZone:gmtTimeZone];
NSDate *date = [formatter dateFromString:dateString];
if (date == nil)
{
NSString *UTC_FORMAT2 = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSSSSX5";
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:UTC_FORMAT2];
[formatter2 setLocale:enUSPOSIXLocale];
[formatter2 setTimeZone:gmtTimeZone];
date = [formatter2 dateFromString:dateString];
}
To avoid dependencies on the current locale, add :
NSString *dateString = #"2014-04-27T04:20:07.000-04:00";
NSString *UTC_FORMAT = #"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:UTC_FORMAT];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [formatter dateFromString:dateString];