I am using the NSDateFormatter's dateFromString, yet when it is given the string '2013-03-06' it spits out an NSDate of '2012-03-06 00:00:00 CST'. The time is of no importance. I have the date format set to be exactly the same between the input and output.
I have an object which gets a date set using the code:
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
NSDate* currentDate = [[NSDate alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
[self setMyValue:[dateFormat stringFromDate:currentDate]];
And then this value is later used to to set the date on a UIDatePicker:
NSDate *date = [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
date = [dateFormat dateFromString:myFilter.myValue];
[datePicker setDate:date animated:YES];
Any pointers on where I am erring would be much appreciated.
You should use yyyy and not YYYY.
From Data Formatting Guide:
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
Change YYYY in the format to yyyy.
Use yyyy instead of YYYY, to get the zero error while changing 'YEARS'.
Related
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.
I am using the NSDateFormatter's dateFromString, yet when it is given the string '2013-03-06' it spits out an NSDate of '2012-03-06 00:00:00 CST'. The time is of no importance. I have the date format set to be exactly the same between the input and output.
I have an object which gets a date set using the code:
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
NSDate* currentDate = [[NSDate alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
[self setMyValue:[dateFormat stringFromDate:currentDate]];
And then this value is later used to to set the date on a UIDatePicker:
NSDate *date = [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
date = [dateFormat dateFromString:myFilter.myValue];
[datePicker setDate:date animated:YES];
Any pointers on where I am erring would be much appreciated.
You should use yyyy and not YYYY.
From Data Formatting Guide:
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
Change YYYY in the format to yyyy.
Use yyyy instead of YYYY, to get the zero error while changing 'YEARS'.
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.
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.
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];