I am trying to create the formatter for parsing Fri, 12 Feb 2016 17:09:53 +0800 (SGT) date format.
I tried using EEE, dd MMM yyyy HH:mm:ss Z, but the output was null.
Any ideas as to how to parse the above date using NSDateFormatter.
Thank you in advance.
P.S - Tagged in Gmail API, since I was receiving the same from its response.
set Date format as
Uppercase Z for a numeric time zone offset
EEE, dd MMM yyyy HH:mm:ss ZZZ (zzz)
for additional Information see this link
Its better to handle Timezone separate from NSDate as NSDate store a point in time with either your local timezone or UTC timezone. Its upon you to handle these timezone.
So I would recommend that you cut off the time zone in parenthesis and just parse the numerical time zone offset before it. That way, you can convert all strings into an NSDate instance in UTC and you shouldn't have any problems parsing the strings.
Use this format: #"ccc, d LLL YYYY HH:mm:ss Z"
Reference took from and for more help regarding NSDateFormatter you can create your date format more easily from this link.
Related
I want to make an NSDateFormatter object for this date format (for example):
2016-04-15 15:00:00
I've tried yyyy-mm-dd hh:mm:ss but then when I am trying to attach it to a date like that:
date = [formatter dateFromString:dateString];
It gives me nil.
what is the correct way to do it?
Thank you!
set date format as
2016-04-15 15:00:00
yyyy-MM-dd HH:mm:ss
your date string is 24 hour format
Here's all you need for date formatting:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
There's a list of link under the Fixed Format title where you can see in detail the different formats for whatever you want, for iOS7 and greater see this:
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
I have searched over internet for a long time to get this but I can't find the solution. I have received a date string from web services as "22 May 2014", I have to convert into NSDate format for check it with current date. And I have to find out the date from web service is in future or in past time.
The actual problem is that when I convert this using
NSDate *date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM YYYY"];
date = [dateFormatter dateFromString:dateString];
But I get an entirely Different Date, Sample Input dateString:22 June 2014 and Output I get is 2013-12-21 18:30:00+0000
Please suggest any solutions.
Thanks in advance. :)
You're using YYYY, which doesn't mean what you think it means. From the TR35-31 documentation, Y is the symbol for "year in week-of-year calendars".
You want dd MMMM yyyy instead as your format string. Mixing week-of-year-based fields and regular day/month/year fields is a recipe for odd problems.
Additionally, you may well want to set the time zone in your formatter - if you're just parsing a date, then you should consider using UTC, and make sure that all your calculations and formatting/parsing use UTC.
(I suspect the issue here is that week-of-year hasn't been set, so is assumed to be 1... and the week-year 2014 started on December 30th. Then the day-of-month is set to 22 by the dd part, and then your time zone offset of UTC+05:30 is taken into account.)
I want to format my date as follows:
Mon, Apr 14 '09
The dateFormat I'm setting for my NSDateFormatter is EEE, MMM dd 'yy but it shows:
Mon, Apr 14 'yy
If I take out the single quote before the year, I get the last two digits of the year but it's not obvious that it's the year because the single quote is gone. Help?
I've also tried putting ''', '\'', and '\''' before 'yy' but it doesn't work, although the last two produced 'yy and ''09
From http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns:
In patterns, two single quotes represents a literal single quote, …
In your case:
[formatter setDateFormat:#"EEE, MMM dd ''yy"];
I am trying to convert milliseconds into date. Below shown is the code i am using.
double startDateDb=1380275880000;
NSDate *date=[NSDate dateWithTimeIntervalSince1970:(startDateDb/1000.0)];
NSLog(#"date---%#",date);
My log gives date as 2013-09-27 09:58:00 +0000
When i use online tool to convert i am getting "9/27/13 5:58 AM" which is correct.
Please help me to fix the issue.
NSLog used your timezone when displaying dates. TO get the date in UTC use the NSDate methods and specify the tie zone. All NSDates are UTC timezone based.
Use NSDateFormatter to display the date/time in another timezone.
Your NSLog put the timezone in GMT, the timezone you're looking for is GMT-4.
I have this date:
2013-07-15T06:07:53-04:00
I use NSDateFormatter to convert this to NSDate:
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
This works but it doesn't recognize the timezone. It returns:
2013-07-15 10:07:53 +0000
What am i doing wrong?
Thanks
The log output is correct. NSLogging an NSDate will always show that date/time in UTC (see how your output has +0000 instead of -04:00). NSDate objects represent a specific moment in time regardless of calendar and timezones. You use this "moment in time" in conjunction with a formatter (with a timezone set) in order to format that "moment in time" into a locale-specific time.
If you want to see log the output with your own timezone:
// pick a different timezone if necessary
[formatter setTimeZone:[NSTimeZone localTimeZone]];
NSLog(#"%#", [formatter stringFromDate:momentInTime];
isn't the "-04:00" at the end of the string telling NSDateFormatter that this date is GMT-4 and thus, it's just converting it to GMT ?
If not, please provide the full initialization of your NSDateFormatter.
Try this instead:
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
more info here
iPhone NSDateFormatter Timezone Conversion
5 ZZZZZ - heres a category I wrote with some sample of GMT to BST
https://github.com/clearbrian/NSDateFormatter_ISO_8601