Issue with NSDateformatter while converting NSDate to format "MMM, YYYY" - ios

I have NSDate with value 2015-12-27 +0000 and when I convert this NSDate to NSString with format MMM, YYYY I am getting:
NSString as Dec, 2016

This is because the capital Y in your date format specifies the year in the ISO week date system, not the Gregorian calendar.
I imagine your code may look something like:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM, YYYY"];
NSLog(#"Date: %#", [df stringFromDate:yourDate]);
Instead try using the format:
[df setDateFormat:#"MMM, yyyy"];
When using format strings such as this to specify your output format, NSDateFormatter uses the conventions from Unicode Technical Standard #35 (which describes the difference between Y and y if you want more detail).

Related

Date giving wrong year on Device [duplicate]

What is exact difference between 'YYYY' and 'yyyy'. I read in this link, it states that
A common mistake is to use YYYY. yyyy specifies the calendar year
whereas YYYY specifies the year (of “Week of Year”), used in the ISO
year-week calendar. In most cases, yyyy and YYYY yield the same
number, however they may be different. Typically you should use the
calendar year.
But when I try to use
NSString *stringDate = #"Feb 28, 2013 05:30pm";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mma"];
NSDate *date=[dateFormatter dateFromString:stringDate];
NSLog(#"Date 1 : %#",date); //2013-02-28 12:00:00 +0000
NSString *stringDatee = #"Feb 28, 2013 05:30pm";
NSDateFormatter *dateFormatterr = [[NSDateFormatter alloc] init];
[dateFormatterr setDateFormat:#"MMM dd, YYYY hh:mma"];
NSDate *datee=[dateFormatterr dateFromString:stringDatee];
NSLog(#"Date 2 : %#",datee); //2013-01-05 12:00:00 +0000
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd, YYYY hh:mma"];
NSString *dateString = [dateFormat stringFromDate:datee];
NSLog(#"date 3 : %#", dateString); //Jan 05, 2013 05:30PM
As here, result to date and datee different, which I understood, but why result of date 2 and date 3 are different? As I am creating date from string and reversing same to string again, but output mismatches?
Has anybody knows reason about same?. Though it specifies week of year, still I should get result same.
Thanks..
EDIT :-
If I code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd, YYYY hh:mma"];
NSString *dateString = [dateFormatterr stringFromDate:[NSDate date]];
NSLog(#"date: %#", dateString); //Feb 28, 2013 04:37PM
If results me proper result, but same which I pass as string to date I get 2013-01-05 12:00:00 +0000, check date 2 of NSLog, Strange result, why?
Also when using a date format string using the correct format is important.
#"YYYY" is week-based calendar year.
#"yyyy" is ordinary calendar year.
You can go through the whole blog, its a good to give it a look
https://web.archive.org/web/20150423093107/http://realmacsoftware.com/blog/working-with-date-and-time
http://realmacsoftware.com/blog/working-with-date-and-time (dead link)
A common mistake is to use
YYYY. yyyy specifies the calendar year whereas YYYY specifies the year
(of “Week of Year”), used in the ISO year-week calendar. In most
cases, yyyy and YYYY yield the same number, however they may be
different. Typically you should use the calendar year.
from Apple Docs
dd/MMM/YYYY - e.g.:1 01/Jan/2000; answer : 19/dec/1999
(see weekly calendar December month last Monday
suppose leaf year + 1 day)
dd/MMM/yyyy - eg: ordinary - no problem.
All answers differentiating yyyy and YYYY are right answers for another question. The question itself refers to another thing.
Why does these two values are different? (extracted from question)
NSLog(#"Date 2 : %#",datee); //2013-01-05 12:00:00 +0000
NSLog(#"Date 3 : %#", dateString); //Jan 05, 2013 05:30PM
The answer here #P.J is that they are not really different in value. When you log an NSDate (which is Date 2) you are getting the full description of your object which happens to be on UTC Timezone. This logic does not happen when logging Date 3 because it was already converted to a String and applied your Timezone.
For printing Date 3 the 'same way' as you are getting Date 2. You should specify UTC TimeZone for Date 3. Something like this :
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd, YYYY hh:mma"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *dateString = [dateFormat stringFromDate:datee];
NSLog(#"date 3 : %#", dateString);
Hope this helps.
tl;dr the Timezone

Parse NSDate with format #"MMM dd ''YY"

I have a date string which looks like this: Mar 13 '15
I am not able to find the right way to parse this. I've tried the following:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd ''YY"];
//[df setDateFormat:#"MMM dd 'YY"];
//[df setDateFormat:#"MMM dd YY"];
NSDate *date = [df dateFromString:label.text];
Does anyone know how to do it using setDateFormat ?
Thanks^^
YY is for year in week based calendars, which is used in some non-gregorian calendars. You generally should use yy instead. See the unicode reference of valid date pattern.
And you should set the locale to en_US_POSIX, to make sure that the date is parsed as a date in the english language. For example in the german locale your conversion would fail because instead of Mar 13 '15 today is März 13 '15.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd ''yy"];
[df setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
You are using the wrong style of year, you need to use lowercase y.
[df setDateFormat:#"MMM d ''yy"];
Uppercase Y is documented as "Year (in "Week of Year" based calendars)." and mentions "May not always be the same value as calendar year". See:
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

How to format the date to IST?

I have to format the date as below:
19-JUL-2014 10:27:16 IST
How can I do that? Should I send "IST" as string object?
I tried -
NSDate* sourceDate = [NSDate date];
NSLog(#"Date is : %#", sourceDate);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSLog(#"TimeZone is : %#", currentTimeZone);
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init] ;
dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm:ss Z"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"%#",[dateFormatter stringFromDate:sourceDate]);
I've tried several of scenarios of timezone formatters according to the Apple's official docs and the Unicode date-formatter standards.
I inited the timezone like this:
NSTimeZone *_timezone = [NSTimeZone timeZoneWithName:#"IST"];
that presented the proper timezone to me with +0530 offset, so that was used for the instance of my NSDateFormatter.
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc]init];
[_dateFormatter setTimeZone:_timezone];
here is the list about I've experienced with different format-scpecifiers:
z = GMT+0530 IST
zz = GMT+0530 IST
zzz = GMT+0530 IST
zzzz = India Standard Time IST
zzzzz = India Standard Time IST
it seemed that none of the standard format-specifiers could provide the actual "IST" only as string, a match was the "India Standard Time IST" with format specifier zzzz and zzzzz – but you can see the "GMT+0530 IST" still contains it with the rest of the formatters.
NOTE: the other format specifiers like Z, v, V, x or X did not seem useful either.
I've read more about format specifiers, the docs says about using z:
The short specific non-location format (e.g. PDT). Where that is unavailable, falls back to the short localized GMT format.
that means to me, the actual short specific non-location format for India Standard Time is not available via NSDateFormatter directly – or for some reason is specified as "GMT+0530 IST" not as short "IST"†.
on the other hand, I'm not sure whether the long specific non-location format is accepted on your server side (aka "India Standard Time IST"), or the timezone must be marked by string "IST" only.
I'm afraid if that latest format is expected only you will need to add it manually and inelegantly, like:
[_dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm:ss"];
NSString *_date = [[_dateFormatter stringFromDate:[NSDate date]] stringByAppendingString:#" IST"];
NOTE: I've also spotted the months' names should be capitalised as well, I'm not sure that is another expectation or the generic capitalisation of months' names (like e.g. "Jul", "Sep" etc...) is good enough for your server side – I did not take care of capitalising them in my current answer.
† I have not found any standard which to be supposed to describe the actual short format, so based on the unicode standards I would assume the "IST" should be the shortened format against the "GMT+0530 IST" – but that is based on my personal speculation only.
NSDate* sourceDate = [NSDate date];
NSLog(#"Date is : %#", sourceDate);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSLog(#"TimeZone is : %#", currentTimeZone);
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init] ;
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm:ss zzz"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"%#",[dateFormatter stringFromDate:sourceDate]);
This may help you
Please try this,
NSDate *date= [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"dd-MMM-yyyy HH:mm:ss z"];
NSLog(#"%#",[dateFormatter1 stringFromDate:date]);

Difference between 'YYYY' and 'yyyy' in NSDateFormatter

What is exact difference between 'YYYY' and 'yyyy'. I read in this link, it states that
A common mistake is to use YYYY. yyyy specifies the calendar year
whereas YYYY specifies the year (of “Week of Year”), used in the ISO
year-week calendar. In most cases, yyyy and YYYY yield the same
number, however they may be different. Typically you should use the
calendar year.
But when I try to use
NSString *stringDate = #"Feb 28, 2013 05:30pm";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mma"];
NSDate *date=[dateFormatter dateFromString:stringDate];
NSLog(#"Date 1 : %#",date); //2013-02-28 12:00:00 +0000
NSString *stringDatee = #"Feb 28, 2013 05:30pm";
NSDateFormatter *dateFormatterr = [[NSDateFormatter alloc] init];
[dateFormatterr setDateFormat:#"MMM dd, YYYY hh:mma"];
NSDate *datee=[dateFormatterr dateFromString:stringDatee];
NSLog(#"Date 2 : %#",datee); //2013-01-05 12:00:00 +0000
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd, YYYY hh:mma"];
NSString *dateString = [dateFormat stringFromDate:datee];
NSLog(#"date 3 : %#", dateString); //Jan 05, 2013 05:30PM
As here, result to date and datee different, which I understood, but why result of date 2 and date 3 are different? As I am creating date from string and reversing same to string again, but output mismatches?
Has anybody knows reason about same?. Though it specifies week of year, still I should get result same.
Thanks..
EDIT :-
If I code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd, YYYY hh:mma"];
NSString *dateString = [dateFormatterr stringFromDate:[NSDate date]];
NSLog(#"date: %#", dateString); //Feb 28, 2013 04:37PM
If results me proper result, but same which I pass as string to date I get 2013-01-05 12:00:00 +0000, check date 2 of NSLog, Strange result, why?
Also when using a date format string using the correct format is important.
#"YYYY" is week-based calendar year.
#"yyyy" is ordinary calendar year.
You can go through the whole blog, its a good to give it a look
https://web.archive.org/web/20150423093107/http://realmacsoftware.com/blog/working-with-date-and-time
http://realmacsoftware.com/blog/working-with-date-and-time (dead link)
A common mistake is to use
YYYY. yyyy specifies the calendar year whereas YYYY specifies the year
(of “Week of Year”), used in the ISO year-week calendar. In most
cases, yyyy and YYYY yield the same number, however they may be
different. Typically you should use the calendar year.
from Apple Docs
dd/MMM/YYYY - e.g.:1 01/Jan/2000; answer : 19/dec/1999
(see weekly calendar December month last Monday
suppose leaf year + 1 day)
dd/MMM/yyyy - eg: ordinary - no problem.
All answers differentiating yyyy and YYYY are right answers for another question. The question itself refers to another thing.
Why does these two values are different? (extracted from question)
NSLog(#"Date 2 : %#",datee); //2013-01-05 12:00:00 +0000
NSLog(#"Date 3 : %#", dateString); //Jan 05, 2013 05:30PM
The answer here #P.J is that they are not really different in value. When you log an NSDate (which is Date 2) you are getting the full description of your object which happens to be on UTC Timezone. This logic does not happen when logging Date 3 because it was already converted to a String and applied your Timezone.
For printing Date 3 the 'same way' as you are getting Date 2. You should specify UTC TimeZone for Date 3. Something like this :
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd, YYYY hh:mma"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *dateString = [dateFormat stringFromDate:datee];
NSLog(#"date 3 : %#", dateString);
Hope this helps.
tl;dr the Timezone

Convert NSString back to NSDate

I am using NSDateFormatter to convert the current date to a string (in the format: February 16, 2013). How can I convert this string back to a NSDate object?
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterNoStyle];
The problem appears to be that the month is written out (February vs. 02), and other questions only explain to use NSDateFormatter with a format such as MM-dd-yyyy, which I do not believe is possible here. Must I parse this date manually, convert February to 02, and go from there?
You can use dateFromString of the same NSDateFormatter class to perform backward conversion.
To make it work you need to define dateStyle, so parser will know how text string should be parsed. For the date style that you provided code below will work:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:#"February 16, 2013"];
NSLog(#"%#", date);
If you want to be able to use localized date formats, you should go with templates
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
NSLog(#"%#", dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:#"MMMdY"
options:0
locale:[NSLocale currentLocale]]];
NSLog(#"%#", [dateFormatter dateFromString:dateString]);
Since you have a fixed format that you wish to parse, you must setup the date formatter with the locale of en_US_POSIX. Then you must set he date format to MMMM dd, yyyy. This will pare any date string that has the full month name, the month day, a comma, then the four-digit year.

Resources