This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting date from [NSDate date] off by a few hours
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"M-d-yyyy H:mm"];
NSDate *start= [dateFormatter dateFromString:#"10-24-2012 12:15"];
NSDate *end = [dateFormatter dateFromString:#"10-24-2012 15:30"];
When I print out
NSLog(#"------main_event start %#", start);
NSLog(#"-----main_event end %#", end);
The result is
---main_event start 2012-10-24 19:15:00 +0000
---main_event end 2012-10-24 22:30:00 +0000
Now, it looks like the time added 7 hours automatically, 12:15 becomes 19:15, and 15:30 becomes 22:30.
Why?
because the timezone, where your device is located, is UTC-7.
The output is in UTC (hence the +0000), as a single NSDate will always print out it's time in UTC.
If you use an NSDateFormatter to output the date, it will take your locale in account. See my answer here: NSDate date method returns wrong result
These are correct results. When you use NSLog to output an NSDate object, it displays in GMT. The parsing was done in your local timezone. NSDate objects are alway in GMT. If you want to print the NSDate object in your local timezone then you need an NSDateFormatter to print the date.
Related
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 7 years ago.
Important update: Of you hover over a NSDATE with your mouse the degugger will convert the NSDate to your local timezone you have set on you Mac, but if you do a NSLOG you will notice that the NSDate is using the timezone that you assigned to the its respective formatter.
If you want to see in the xcode debugger what the NSDate is for the timezone you are working with go to your date/time settings for you Mac OS and change the Timezone to the one you are testing.
I require a NSDate to be created from the date I pass in, but currently it is set to the the day before I pass in:
NSString *dateStr = #"2015-08-09";
NSDateFormatter *myformatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:dateStr];
The code above returns: an NSDate set to '2015-08-08 12:00:00 +0000'
I need an NSDate object set to the datStr I pass in.
This perhaps, from this:
Convert NSDate to NSString with NSDateFormatter with TimeZone without GMT Time Modifier
leave the 'z' lowercase for named timezone, i.e. PST and uppercase 'Z' for -0800
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMMM dd, yyyy (EEEE) HH:mm:ss z Z"];
NSDate *now = [NSDate date];
NSString *nsstr = [format stringFromDate:now];
Also, you date should be more robust, like if you want to pass in the current day that your are passing in and it's one day off, then just add a day. The problem, it seems is that the date is returning the correct date which is the end of the last day, add 1 second or a millisecond and it'll probably be corrected, or just hack attack this and add 1 day to the days you are passing in. Be smart! Sometimes you just have to add 1 day to fix stuff and move on.
This question already has an answer here:
returns a date an hour in the future
(1 answer)
Closed 8 years ago.
I will appreciate if someone could please advise me what is wrong with the date formatting. I have the following date as string from webservice :
20141211200300 //yyyyMMddHHmmss -->2014/12/11 20:03:00
But when I format it using the following format it adds 2 hrs
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddHHmmss"];
NSDate *date = [dateFormatter dateFromString:localDate];
The output I get in console is
2014-12-11 10:03:00 +0000
I tried adding GMT timezone as well as Australia/Brisbane still get the same issue.
You did not show the code that printed the number. If you used NSLog() there is probably a time zone issue, Note that the printed time is in GMT. Is your time zone two hours from GMT?
I have written the following method:
- (NSDate *)stringToDate:(NSString *)dateString
{
// Convert string to date
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"America/New_York"];
[dateFormat setDateFormat:#"M/d/yyyy HH:mm:ss"];
[dateFormat setTimeZone:tz];
NSDate * date = [dateFormat dateFromString:[NSString stringWithFormat:#"%# 00:00:00", dateString]];
return dateOnly;
}
When I call this with just a date such as 11/1/2013 or 11/13/2013 I get 2013-11-01 04:00:00 +0000 and 2013-11-13 05:00:00 +0000.
If I set a breakpoint on the return the date appears right, but if I break at in the calling function after this call, the date is returned with the time.
How come my time is not always 0. Can anyway tell me what is wrong in my function?
Thank you
UPDATE:
The input string is as follows: 11/1/2013 and 11/13/2013
NSDate is a point in time. It will always have a time component.
And if not printed as a string form a NSDateFormatter, the Date and time will always be the one of UTC/GMT.
The format and the date string must fit.
NSString *dateString = #"11/1/2013";
[dateFormat setDateFormat:#"M/d/yyyy"];
The one hour apart comes from the Daylight saving time. Till November, 3rd 2013 New York has Summer time. http://www.timeanddate.com/worldclock/clockchange.html?n=179
Ok, so can I ignore that? I am trying to compare NSDates when I do my comparison fails because of the time part
You should create dates with with a time during the day — i.e. noon — to be save of DST mess and compare those. Use NSComponents for that.
A must-see for any iOS/Cocoa-Developer: the fantastic WWDC 2011 Video "Session 117 - Performing Calendar Calculations".
I am printing NSDate like this:
NSDate *date = [NSDate date];
NSString *stringDate = [data description];
Right now, it's July 1, 2011 11:43 pm. My iPod even says that on the top bar. But stringDate prints out: 2011-07-02 03:43:46 +0000
This is obviously wrong. I have used NSDate millions of times but never had this problem. What could be wrong?
Thanks
Your region's time offset is -04:00? NSDate will automatically adjust the time offset when displaying the date. Try,
NSString *str = [date descriptionWithLocale:[NSLocale currentLocale]];
It will show you the correct date.
Are you in the GMT -4 time zone? The result that it is giving you would be correct in that case, as 2011-07-02 03:43:46 +0000 is 2011-07-01 11:43:46 -4000.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date1 = [dateFormatter dateFromString:#"1213-02-27"];
NSTimeInterval nsdateResult = [date1 timeIntervalSince1970];
NSTimeInterval sqliteResult = -23883638400;
GHAssertEquals(sqliteResult, nsdateResult, #"sqlite and nsdate should agree");
The expected number is what SQLite tells me should be the result, but this test fails since Cocoa tells me -23883033600 instead of -23883638400...i.e. 4 days later!
What gives? Am I doing something wrong or is either SQLite or Cocoa wrong?
Edit
Just checked the math and it seems Cocoa is giving me a wrong value: according to Wolfram Alpha, there are 276431 days between 2/27/1213 and 1/1/1970. Take this times 24*60*60, and you get the value given by SQLite... so am I doing something wrong or is this some kind of Cocoa bug?
Edit 2
Fvu is right, it has to do with the Gregorian/Julian craziness back in 1582.
NSDate *date1 = [dateFormatter dateFromString:#"1582-10-15"];
NSDate *date2 = [dateFormatter dateFromString:#"1582-10-14"];
NSTimeInterval i = [date2 timeIntervalSinceDate:date1];
Surprisingly, i is 777600, i.e. 10/14 is 9 days LATER than 10/15 because it automatically is viewed as a Julian date whereas 10/15 is viewed as a Gregorian date. So basically what I will need to do is convert any dates before 10/15 from the Julian to the Gregorian calendar.
Your sample date predates the Gregorian calendar so I think some oddities can be expected. However, as at the introduction of the Gregorian calendar the date advanced by 10 days I don't have a bulletproof explanation for the calculated 4 day difference.