I am trying to create datefromstring. However, the date is just wrong that is created. Can anyone see anything wrong with my code?
this is the code:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MM/dd/YYYY hh:mm"];
[format setTimeZone:[NSTimeZone localTimeZone]];
NSString *datePart = [txtView.text substringToIndex:16];
NSLog(#"datepart: '%#'", datePart);
NSDate *midDate = [format dateFromString:datePart];
NSLog(#"format middate: %#", [format stringFromDate:midDate]);
this is the output:
datepart: '04/29/2013 04:23'
format middate: 01/05/2013 04:23
It is certainly not January by any means - why is this so incorrect??
Thanks!
R
From the Date Formatting Guide documentation:
[This example] uses yyyy to specify the year component. 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.
I've give the code sample a run using both YYYY and yyyy, and yyyy yields the correct result.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MM/dd/yyyy hh:mm"];
[format setTimeZone:[NSTimeZone localTimeZone]];
NSString *datePart = #"04/29/2013 04:23";
NSLog(#"datepart: '%#'", datePart);
NSDate *midDate = [format dateFromString:datePart];
NSLog(#"format middate: %#", [format stringFromDate:midDate]);
Prints out
2013-04-29 21:40:42.450 Untitled[95290:707] datepart: '04/29/2013 04:23'
2013-04-29 21:40:42.453 Untitled[95290:707] format middate: 04/29/2013 04:23
Fun Fact!
This was the cause of the Do Not Disturb bug Apple had at the start of 2013, they used YYYY instead of yyyy. Some more info here.
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 a problem with Date Format
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"YYYY-MM-dd hh:mm:ssZZ"];
formatedDate =[format stringFromDate:datePicker.date];
Everything works fine, except one thing
when I set the YEAR less than or equal to 1924, DateFormatter returns
1924-04-21 03:00:07+050748
Whereas should be
1924-04-21 03:00:07+0500
What's wrong?
You can see the Apple Data Formatting Guide https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
In the "Fixed Formats",
There are two things to note about this example:
It uses yyyy to specify the year component. 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.
The representation of the time may be 13:00. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be 1:00 pm.
So you should change "YYYY" to "yyyy".
User this method .......
-(NSString*) currentDateForServer
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:UTC];
[dateFormat setTimeZone:timeZone];
[dateFormat setDateFormat:DATE_FORMATE];
NSDate *currentDate = [NSDate date];
NSString *finalString = [dateFormat stringFromDate:currentDate];
return finalString;
}
Define UTC and DATE_FORMATE as you need
You can set your Timezone, like with GMT,
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd hh:mm:ssZZ"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[format setTimeZone:gmt];
formatedDate =[format stringFromDate:datePicker.date];
This may change your time in output string formatedDate, but you can set NSTimeZone as per your requirement.
This question already has answers here:
NSString to NSDate
(5 answers)
Closed 9 years ago.
I'm trying to get the date "30/06/2013" from the button:
I tried this code but I got in the log: 2013-01-04 22:00:00 +0000:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/YYYY"];
NSDate *date = [formatter dateFromString:dateBtn.titleLabel.text];
NSLog(#"date %#",date);
and in the app i got this date "05/01/2013":
I also tried this code but it didn't work:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"he_IL"];
[dateFormat setLocale:locale];
[dateFormat setDateFormat:#"dd/MM/YYYY"];
NSTimeInterval interval = 5 * 60 * 60;
NSDate *date1 = [dateFormat dateFromString:dateBtn.titleLabel.text];
date1 = [date1 dateByAddingTimeInterval:interval];
It is important to note I use locale Hebrew.
Have a look at Apple's official documentation on date formatters. Please note how the format differs slightly across different platforms and versions.
Also note the difference between yyyy and YYYY.
set date format to "dd/MM/yyyy"
NSString *originalDate = #"30/06/2013";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [formatter dateFromString:originalDate];
NSLog(#"date %#",date);
The explanation from #fzwo apple link
It uses yyyy to specify the year component. 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.
Code:
NSString *ds = #"2013-02-25";
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd"];
NSDate* date = [formatter dateFromString:ds];
NSLog(#"%#", date); //2012-02-24 16:00:00 +0000
[formatter setDateFormat:#"MMM dd, YYYY"];
NSLog(#"%#", [formatter stringFromDate:date]); //Feb 25, 2012
Is there something wrong I made? Or iOS 6 SDK bug?
Change this :
[formatter setDateFormat:#"YYYY-MM-dd"];
[formatter setDateFormat:#"MMM dd, YYYY"];
To :
[formatter setDateFormat:#"yyyy-MM-dd"];
[formatter setDateFormat:#"MMM dd, yyyy"];
Reason for the error,
It's a common mistake 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.
I have set the locale and timezone but when I format the date, I always get invalid date. The month is always December and year is one less that the specified year. In my case I dont need the day component.
I checked other similar post but it didn't solved the problem.
Any help is appreciated. Thank you.
Here is the code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setDateFormat:#"MMM YYYY"];
NSDate *formattedDate = [dateFormatter dateFromString:#"Sep 2013"];
NSLog(#"Date: %#", [dateFormatter stringFromDate:formattedDate]);
[dateFormatter release]; dateFormatter = nil;
OUTPUT: Date: Dec 2012
"YYYY" format is used for "Week of Year" based calendars. Use "yyyy" format specifier instead:
[dateFormatter setDateFormat:#"MMM yyyy"];
I just wanted to add something to the great answer by Vladimir. If you do this before setting your locale, it seems that the date formatting goes crazy. What I had to do was to set the locale before setting the new format, and then use the setDateFormat to change the format based on the locale used.So something like this would do:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];//I wanted to set the locale to wherever user is using my app in
[formatter setDateFormat:#"MMM YYYY"];
NSDate *now = [NSDate Date];
NSString *fancyLookingDate = [formatter stringFromDate:now];