NSDateFormatter not giving me correct - objective-c

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]];

Related

NSDate is wrong when doing 'dateFromString'

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.

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.

NSDateFormatter dateFromString with date

i have a problem with method dateFromString, here is my code
NSString* res = [NSString stringWithFormat:#"%#/%#/%#",dateInput.text,monthInput.text,yearInput.text];
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:#"dd/MM/yy"];
NSDate* inpTime = [formatter dateFromString:res];
[dateResult setText:[NSString stringWithFormat:#"%#",inpTime]];
when I run, the date in "inpTime" always is "dateInput" - 1.
for example: if "dateInput" is 5, the date in "inpTime" will be 4
You need to adjust the timezone.
Change
[formatter setTimeZone:[NSTimeZone localTimeZone]];
to
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
This is because you're not setting the time, so it's set by default the midnight UTC.
But when you are displaying the date with a timezone other than UTC the time is shifted accordingly.
As an example if you live in New York the 12/29/2012 00:00:00 UTC is actually the 12/28/2012 18:00:00 for you.
You Code is perfect, no error what so ever.
Try nslogging dateInput.text, monthInput.text and yearInput.text...might be from here you are getting invalid data.

NSDateFormatter not working with 4:15:00 PM

How do I convert "4:15:00 PM" to an NSDate? Below is the code I have:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss aa"];
NSDate *date = [df dateFromString:#"4:15:00 PM"];
NSLog(#"date: %#",date);//outputs: 1970-01-01 17:15:00 +0000
//should output: 1970-01-01 16:15:00 +0000
UPDATE: I updated based on responses and I am still having the incorrect show:
NSLog(#"date: %#",[df stringFromDate:date]);//outputs: 12:15:00 PM
//should output: 4:15:00 PM
The question is why does the time change from 4:15:00 PM 12:15:00 PM.
Actually, this is correct.
However, the debugger and NSLog display the time in GMT.
If you want to display local time, use a NSDateFormatter and stringFromDate.
Update:
Your date formatter format is also set incorrectly.
It should be: [df setDateFormat:#"hh:mm:ss aa"]; since you are using a 12 hour clock.
This is because of your locale.
NSDateFormatter outputs a NSDate - which is always in GMT time.
If you want your initial string interpreted as GMT time then you need to include the timezone.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss aa z"];
NSDate *date = [df dateFromString:#"4:15:00 PM GMT"];

Conversion NSString to NSDate. 1 day is lost

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