Hopefully this is just a quick one and I'm missing something simple...
I've got an NSDateFormatter, which I'm using to convert the string 2011-11-10 into a Date object.
NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:#"YYYY-MM-DD"];
// input of 2011-11-10, output of 2011-01-10 00:00:00 +0000
[appointment setDate:[fmtDate dateFromString:
[tempAppointment objectForKey:#"date"]
]];
The return from the NSDateFormatter is stored in a managedObjectContext. My problem is that the dateFormatter is returning the date as 2011-01-10 00:00:00 +0000
Why is it reducing the month from Nobvember to January? It's retaining the year and the day fine, but not the month.
Do I need to include the hours when I store the date? Or is it something to do with the format I set?
Try the format #"yyyy-MM-dd" for the format as specified here in the "Use Format Strings to Specify Custom Formats" section. It has an example and list YYYY as a common mistake.
Your date format string is not correct using YYYY. Try yyyy-M-d or yyyy-MM-dd
NSString *dateString = #"2011-11-10";
NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:#"yyyy-M-d"];
NSDate *date = [fmtDate dateFromString:dateString];
NSLog(#"date: %#", date);
Outputs:
2011-11-10 20:01:40.638 Craplet[81514:707] date: 2011-11-10 05:00:00 +0000
See:
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
Related
This question already has an answer here:
NSDateFormatter show wrong year
(1 answer)
Closed 6 years ago.
My code:
NSString *dateStr = #"03-02-2017";//[responseObject objectForKey:#"event_date"];
NSLog(#"'%#'", dateStr);
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_HK"];
[dateFormat setLocale:locale]; //To fix the format into something like: 10 Feb 2017, but not: 10 2月 2017
[dateFormat setDateFormat:#"dd-MM-YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"'%#'", date);
// Convert date object to desired output format
[dateFormat setDateFormat:#"dd MMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"'%#'", dateStr);
And in log it returns
'03-02-2017'
'2016-12-24 16:00:00 +0000'
'25 Dec 2016'
Anyone know why this happened? I searched in google that most cases are causing by timezone. However, I'm pretty sure it's not about timeZone because the difference of dates are too large, but I can't figure out the root of this problem.
I think your dateformat is wrong.
It should be
dd-MM-yyyy
I found that my format is wrong from answer objective-c - NSDateFormatter returns wrong date
and reference by http://nsdateformatter.com .
It should be "yyyy" instead of "YYYY"
I have a date i take from twitter :
NSString *date=[twt objectForKey:#"created_at"];
NSLog(#"%#",date);
NSDateFormatter *df = [ [NSDateFormatter alloc] init] ;
[df setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSDate *newdate = [df dateFromString:date];
NSLog(#"new: %#",newdate); //null
the date is : Thu Jun 12 20:56:53 +0000 2014 and i get null on the new date .
What am i missing ?
the date formatter for that format (Thu Jun 12 20:56:53 +0000 2014) would be this:
"EEE MMM d HH:mm:ss Z yyyy"
rather than this "dd-MM-yyyy HH:mm:ss".
The date formatting string you've shared sure does't look like it matches the date you're getting from Twitter. The docs for dateFromString state...
A date representation of string interpreted using the receiver’s
current settings. If dateFromString: can not parse the string, returns
nil.
Read up!
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
I wrote a code block
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSLog(#"date:%#,string:%#", aDate,[dateFormatter stringFromDate:aDate]);
it works well for most date, but if the date is 2013-12-30 it works strange .
the Log string is date:2013-12-30 16:00:00 +0000,string:2014-12-31
Why? why the date 2013-12-30 convert to string is 2014-12-31?
The NSDate you're giving it is 4pm in GMT. You're probably not in GMT, so it's formatting that time/date for your current timezone, where it's at least eight hours later.
Create your NSDate with the local timezone, or use NSDateComponents if you really want just a date with no time.
I have a method,
+ (NSDate *) convertToDateFrom:(NSString *) dateString
{
if (dateString == nil || [dateString isEqual:#""]) return nil; //return nil if dateString is empty
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMMM yyyy HH:mm"];
NSDate *date = [df dateFromString:dateString];
return date;
}
When I pass,
#"Monday, 21 November 2011 17:01" //Passed string
It returns a wrong date,
2011-11-21 23:14:00 +0000 // Output
I am not sure whether I am using those flags correctly or NSDateFormatter isn't properly converting my string to date.
Am I doing something wrong?
Thanks
The +0000 at the end of the date indicates GMT. All dates are stored relative to GMT; when you convert a date to a string or vice versa using a date formatter, the offset to your time zone is included. You can use NSDateFormatter's -setTimeZone: method to set the time zone used.
In short, you're not doing anything wrong in your code. Use [df stringFromDate:date]; to see that the date is correct. (You can also use NSDate's -descriptionWithCalendarFormat:timeZone:locale:.)
try using
df stringFromDate:date
Following worked on mine,
NSLog(#"Date for locale %#: %#",
[[dateFormatter locale] localeIdentifier], [df stringFromDate:date]);
gave me output as :
Date for locale en_US: Wednesday, 26 June 2013 15:50
Try setting the time zone and locale.
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString = #"20.10.2010";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSLog(#"%#", [dateFormatter dateFromString:dateString]);
My output is:
2010-10-19 22:00:00 GMT
Why is one day lost?
Probably because your locale specificies that you're in GMT +2.
That means the given date is interpreted as 2010-10-20 00:00 GMT+2, hence in GMT+0 that's 2010-10-19 22:00.
You not lost 1 day, but 2 hours. But the display is GMT.
What do you want to do with your date ?
See the reference to change the output formatter