NSDate adds 2 hrs [duplicate] - objective-c

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?

Related

Parsing a date with [duplicate]

This question already has answers here:
Constructing an NSDate from today's date and a string with the time
(2 answers)
Closed 6 years ago.
I'm parsing a time using NSDateFormatter. How do I move the NSDate object to the current day so that it will be 12:34pm with today's date?
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"h:m a"];
NSDate *startDate = [formatter dateFromString:#"12:34 pm"];
You could get today's date/time from NSDate, then use NSDateComponents to break up both that and your startDate, copy the time components from one to the other, then translate back into an NSDate.
HTH

NSDate being set to the day before [duplicate]

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.

NSDateFromString, parsing a date in the very past

I'm executing this code to parse date 01 January 1800:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"ddMMyyyy"];
NSDate *date = [formatter dateFromString:#"01011800"];
when I print date with
NSLog(#"%#", date);
I get
1799-12-31 23:10:04 +0000
My expected result is 1799-12-31 23:00:00 +0000: I'm in central Europe and at 1st January I have a 1.00 hours offset respect to UTC, but I cannot understand where that 10 minutes and 4 seconds come from.
Have a good look on the internet about time zone changes in your area through the last few hundred years. Bets are that the result is actually correct, and that in 1800 your local time zone was off by ten minutes and four seconds.
For a date in 1800, Apple obviously doesn't use what the timezone information is today, but what it was 214 years ago.

NSDate Output incorrectly [duplicate]

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.

Obj-C, problem formatting date, gaining a day?

I've discovered a bug in my app / function. I pass in a NSString of a date.
The function then uses the NSDateFotmatterShortStyle.
Heres a screen shot of my function in the debugger.
I'd like to end up with a date of 2011-04-18
Not sure why its added 1ppm either, I need it to be 00:00:00
Whats happening and how do I fix this ?
I use the MidnightUTC function from here ( How do I create the current date (or any date) as an NSDate without hours, minutes and seconds? ) to get rid of the hours.
I actually see no connection between the midnightUTC method and your provided code.
Anyway, the problem with your given example is that, the parsing of the string 4/18/11 will default the missing values like minutes etc AND your current time zone, but the string will be assumed as GMT time so this will result in the offset you see.
The solution is to set the time zone for the NSDateFormatter. Look at this code, I've tested it a minute ago, and the console output. aaa reveals the odd offset, bbb does look as expected.
NSDateFormatter *dt = [[[NSDateFormatter alloc] init] autorelease];
[dt setDateStyle:NSDateFormatterShortStyle];
NSDate *aaa = [dt dateFromString:#"4/18/11"];
NSLog(#"1a. %#", [dt timeZone]);
NSLog(#"1b. %#", aaa);
[dt setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *bbb = [dt dateFromString:#"4/18/11"];
NSLog(#"2a. %#", [dt timeZone]);
NSLog(#"2b. %#", bbb);
Console output
1a. Europe/Berlin (CEST) offset 7200 (Daylight)
1b. 2011-04-17 22:00:00 +0000
2a. GMT (GMT+00:00) offset 0
2b. 2011-04-18 00:00:00 +0000
the midnightUTC function you're using creates dates by setting the timeZone to GMT, which is different from UTC half the year. UTC doesn't observe any summer / daylight savings time changes, while GMT does, so [NSTimeZone timeZoneForSecondsFromGMT:0] will be an hour off UTC for roughly half the year.