NSDateFormatter not working with 4:15:00 PM - objective-c

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

Related

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

iOS dateFormatter dateFromString month issue

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

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