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
Related
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).
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
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
I have found numerous timestamp conversions and they all work but the thing is, when I put my code to the text form of the date, I always come out 4 months ahead.
This is pulling the current Day of Week, Date and Time. I have it set this way cause I select the day with a DateTime Picker. This is just my viewDidLoad to pull today's date.
NSDate *myDate = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"cccc, MMMM dd, YYYY, hh:mm aa"];
NSString *prettyVersion = [dateFormat stringFromDate:myDate];
date.text = prettyVersion;
Now comes the timestamp conversion to take prettyVersion to timeIntervalSince1970
NSDate *startdates = [dateFormat dateFromString:prettyVersion];
NSLog(#"Timestamp %0.0f",[startdates timeIntervalSince1970]);
NSLog outputs "Timestamp 1356317580" which when converted is Mon, 24 Dec 2012 02:53:00 GMT
Now I can do this
NSLog(#"Timestamp2 %0.0f",[myDate timeIntervalSince1970]);
and get the right timestamp.
So where am i messing up at.
This question has already been answered here: NSDateFormatter dateFromString gives the wrong date
But the gist of it is in your date dateFormat you should use "yyyy" instead of "YYYY".
From the apple documentation:
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.
[dateFormat setDateFormat:#"cccc, MMMM dd, yyyy, hh:mm aa"];
And then for me it would format to "Sunday, September 15, 2013, 07:24 PM" which is what it was when I ran this to test.
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