NSDate is wrong when doing 'dateFromString' - objective-c

Im here in the UK and when working with dates in iOS they are always out by one hour (one hour behind), what I need is the correct time from an NSDate. Ive done the following, but i get two different times:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"NSDate %#",today);
NSLog(#"Time %#", [dateFormatter stringFromDate:today]);
NSDate*stringDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:today]];
NSLog(#"Time date %#",stringDate);
Here is what is logged:
NSDate 2015-07-01 16:07:22 +0000
Time 2015-07-01 17:07:22
Time date 2015-07-01 16:07:22 +0000
Why is this happening? Am i missing something obvious? Surely if the string date is correct, then doing dateFromString should yield the correct results?
The reason I need an NSDate is so I can get the correct amount of seconds using [myTime timeIntervalSince1970]
2015-07-01 17:07:22 //is the correct date
Im expecting an NSDate object that is correct to my date and time.
Update
The answers below helped me find where I was going wrong, so I changed my approach, I was able to get the current timestamp doing the following:
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSDate *curdate = [dateFormatter dateFromString:timeStamp];
int unix_timestamp = [curdate timeIntervalSince1970];
NSDate* referenceDate = [NSDate dateWithTimeIntervalSince1970: 0];
NSTimeZone* timeZone = [NSTimeZone systemTimeZone];
int offset = (int)[timeZone secondsFromGMTForDate: referenceDate];
int currentTimestamp = unix_timestamp + offset;
NSLog(#"CUrrent time stamp %d",currentTimestamp);

NSDate is an absolute moment of time, it does not have a timezone. The date object you have is correct: it is exactly the moment that code was executed.
If you need string representation of that moment of time in a specific time zone, use stringFromDate: just like you did.
If you need to know number values of hour/minute in a specific time zone, use -[NSCalendar components:fromDate:].
[calendar setTimeZone:...];
NSDateComponents* components = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:date];

The date formatter defaults to the local timezone. If you want a different timezone specify it. NSLog of a date used the 'NSDatedescription` method that defaults to GMT (UTC).
Examining the code:
NSDate *today = [NSDate date];
// Creates today's data in GMT (UTC) All NSDates are referenced to GMT.
NSLog(#"NSDate %#",today); (moved up for explanation ordering)
// NSDate 2015-07-01 16:07:22 +0000
// Displays the date in GMT
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
// Creates a date for matter with the system timezone
NSLog(#"Time %#", [dateFormatter stringFromDate:today]);
// Time 2015-07-01 17:07:22
// Creates a string representation in the system timezone and displays it
NSDate*stringDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:today]];
// Creates a date from the string taking into the system timezone
NSLog(#"Time date %#",stringDate);
// Time date 2015-07-01 16:07:22 +0000
// Displays the date in GMT.

Related

Converting Date and time into number of seconds since 1970

I have the date string "2016-04-01T05:00:00+08:00" and I want to convert it into number of seconds since 1 January 1970. I am doing like this:
NSDateFormatter *parsingFormatter = [NSDateFormatter new];
[parsingFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [parsingFormatter dateFromString:"2016-04-01T05:00:00+08:00"];
NSTimeInterval startTime = [date timeIntervalSince1970];
But in the above example startTime returns nil. Can someone tell me what I am doing wrong? How can I convert it?
Try this
NSDateFormatter *parsingFormatter = [NSDateFormatter new];
[parsingFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [parsingFormatter dateFromString:#"2016-04-01T05:00:00+08:00"];
NSTimeInterval startTime = [date timeIntervalSince1970];
I am getting date "2016-03-31 21:00:00 +0000" UTC format.

NSDateFormatter Error

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.

NSDate and NSDate stringFromDate are inconsistent

I am trying to convert a mysql datetime field to another timezone. The conversion seems correct when I print the date object, however when I print the date as a string the time is incorrect. Source code and output are below.
Source:
edited - print original interval in seconds and string, add timezones
interval = [NSNumber numberWithDouble: seconds];
NSLog(#"interval in ms: %#",interval);
self.dateStamp = [[NSDate alloc] initWithTimeIntervalSince1970:seconds/1000];
if(![currentZone.name isEqualToString: timezone]){ //need to convert
NSDateFormatter *fromTZ = [[NSDateFormatter alloc]init];
[fromTZ setTimeZone:currentZone];
[fromTZ setDateFormat:#"hh:mm yyyy-MM-dd"];
NSLog(#"original date: %#",[fromTZ stringFromDate:self.dateStamp]);
NSDateFormatter *toTZ = [[NSDateFormatter alloc]init];
[toTZ setTimeZone:spotZone];
[toTZ setDateFormat:#"hh:mm yyyy-MM-dd"];
NSString *tempdate = [fromTZ stringFromDate:self.dateStamp];
NSDate *toDate = [toTZ dateFromString:tempdate];
NSLog(#"Date: %#",toDate);
NSLog(#"Date String: %#", [toTZ stringFromDate:toDate]);
}
Output:
interval in ms: 1384193573000
original date: 01:12 2013-11-11 //date in current timezone: America/Phoenix
Date: 2013-11-11 08:12:00 +0000 //date in new timezone: America/New York
Date String: 01:12 2013-11-11
NSDate has no concept of time zone, it is simply an object representing the number of seconds since January 1, 2001 GMT and represented in UTC. No matter what you try and set it to, when you straight log an NSDate its going to give you the same result. The only way to circumvent this is to use the NSDateFormatter which you've already done. Everything you've posted is the expected behavior.
Your server sends the time 1384193573000 (in milliseconds) since 1.1.1970, but not with respect to GMT as it is usually done, but with respect to a different time zone, "America/Phoenix" in your example.
Therefore you have to add a correction first, which is the difference between "America/Phoenix" and GMT:
NSTimeInterval serverTime = 1384193573000/1000.;
NSTimeZone *fromZone = [NSTimeZone timeZoneWithName:#"America/Phoenix"];
NSTimeInterval diff = [fromZone secondsFromGMTForDate:[NSDate dateWithTimeIntervalSince1970:0]];
NSDate *dateStamp = [NSDate dateWithTimeIntervalSince1970:(serverTime - diff)];
This was the crucial step. dateStamp is now a "proper" NSDate object representing
the time sent from the server.
What remains is to display the date. That is done as in the above comments and the other
answer. For example:
NSTimeZone *toZone = [NSTimeZone timeZoneWithName:#"America/New_York"];
NSDateFormatter *toTZ = [[NSDateFormatter alloc]init];
[toTZ setTimeZone:toZone];
[toTZ setDateFormat:#"HH:mm yyyy-MM-dd"];
NSString *result = [toTZ stringFromDate:dateStamp];
// result = 20:12 2013-11-11

NSDateFormatter not giving me correct

I am displaying time. It will show me :TIME :2012-06-18 23:00:00 +0000
But after using NSDateFormatter I do not know why it is giving me 00:00:00 AM
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss a"];
NSLog(#"TIME :%#",self.startDate);
NSDate *date = [NSDate date];
NSString * _startTime = [dateFormatter stringFromDate:date];
NSLog(#"current time : %#",_startTime);
NSString * _startTime1 = [dateFormatter stringFromDate:self.startDate];
NSLog(#"Start time : %#",_startTime1);
[dateFormatter release];
**Result is**
TIME :2012-06-18 23:00:00 +0000
current time : 17:05:41 PM
Start time : 00:00:00 AM
Your first NSLog outputs the date in GMT time (notice the trailing +0000). An NSDateFormatter object will format the date to the specified time zone. Your NSLog statements show that the stored date in self.startDate is exactly 00:00:00 AM in at least one time zone, and the formatter is set to that time zone. The formatter will default to the time zone for the device. You could set the formatter's timezone to 0 seconds from GMT to see 23:00:00 PM out of your last NSLog statement:
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

Why isn't my time zone being saved into my NSDate?

I must initialize an NSDate object from NSString in objective-c. I do it like this:
NSString *dateString = [[webSentence child:#"DateTime"].text stringByReplacingOccurrencesOfString:#"T" withString:#" "];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Budapest"]];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
E.g: when I try it with string value #"2011-01-02 17:49:54" I get an NSDate 2011-01-02 16:49:54 +0000. As you can see there is a one hour difference between the two values. NSDate has a wrong value, it should be exactly the same I defined in my string in the timezone I set in dateFormatter. It seems it uses my date defined it string as UTC, even if I set its timezone to "Europe/Budapest". How can I fix this problem?
Thanks!
NSDate stores dates relative to a standard reference date. From the class docs:
"The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. This method returns a time value relative to an absolute reference dateā€”the first instant of 1 January 2001, GMT."
NSDate does not itself have any concept of time zones. So the NSDateFormatter did the right thing: it converted a date which you told it had a GMT offset (by specifying a time zone), and gave you a "normalized" NSDate for that date.
If you want to see the date represented in the Europe/Budapest time zone, either use your existing date formatter (-stringFromDate:) or the appropriate NSDate description method (e.g. -descriptionWithCalendarFormat:timeZone:locale:).
P.S.- You don't need an alloc/init at all in your code as written. In non-ARC that would be a leak.
P.P.S.- Your date format is incorrect and giving nonsensical results. I've gone ahead and cleaned up your code as follows (tested under ARC):
NSString *dateString = #"2011-09-02 17:49:54";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"Europe/Budapest"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:tz];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
NSLog(#"%#", [date descriptionWithCalendarFormat:nil timeZone:tz locale:nil]);
Two things:
1) you have an error in your date format string. You should use MM for month, not mm (lowercase mm is for minutes)
2) after you create you NSDate object, you'll need to use the NSDateFormatter method stringFromDate: to generate a date string localized to a particular timezone. If you just do a straight NSLog() on the NSDate object it will show the date as GMT by default (GMT is one hour behind Budapest time)