Invalid Date Format NSDate

I have an NSString (for example, "2011-04-12 19:23:39"), and what I did to format it in NSDate was the following:

[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date = [inputFormatter dateFromString:newDateString]; 

but what it outputs when i nslog date:

2011-04-12 23:23:39 +0000

which is about 4 hours. Did I miss something? Perhaps a problem with the time zone?

+8
objective-c iphone nsdate nsdateformatter
source share
5 answers

NSDateFormatter use the current device time zone when it created the NSDate object. NSDate stores date / time in GMT. Therefore, by default, NSLog displays the date / time in GMT + 0. So, there is nothing wrong with your code. Now, if you want to display NSDate in the current time zone, you will have to use the NSDateFormatter object.

+2
source share

In short, the GMT date is returned unless otherwise specified. You can set the time zone to get the correct date. If you plan to use the date in the application to set something (for example, localNotification time or Event), you will need to do something special with the date, because if you set the date in iPhone, it will be set as GMT and will be off for a few hours. (in your case 4 hours). I am doing what I just described in one of my applications.

I made a mess trying to make it work correctly without turning off the clock. It was a huge PITA to sort out, but now it works. I copied, pasted, and edited my code for sharing. Again, its a mess, but it works! PickerChanged receives information from UIDatePicker

Using the code below. To answer your question, you can stop at "destinationDate". This will return you the corrected time for your current time zone. I just provided an extra fee when you tried to use the date on the phone somewhere.

NOTE. For a quick example, I put the event reminder in the same function as the datepicker, you DO NOT want to do this, otherwise you will have many reminders set every time the wheels scroll in the datepicker.

The code is below.

  - (void)pickerChanged:(id)sender { NSLog(@"value: %@",[sender date]); NSDate* date= [sender date]; NSDateFormatter *formatter=[[[NSDateFormatter alloc]init]autorelease]; [formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]; [formatter setTimeZone:[NSTimeZone systemTimeZone]]; [formatter setTimeStyle:NSDateFormatterLongStyle]; NSString *dateSelected =[formatter stringFromDate:date]; NSString *timeZone = [dateSelected substringFromIndex:12]; NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; //here we have to get the time difference between GMT and the current users Date (its in seconds) NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date]; //need to reverse offset so its correct when we put it in the calendar correctedTimeForCalendarEvent = destinationGMTOffset + (2*(-1*destinationGMTOffset)); //date to enter into calendar (we will use the correctedTimeForCalendarEvent to correct the time otherwise it will be off by a few hours ) NSDate * destinationDate = [[[NSDate alloc] initWithTimeInterval:destinationGMTOffset sinceDate:date] autorelease]; NSDate * dateForReminder = destinationDate; // return destinationDate; NSLog(@"value: %@ - %@",destinationDate,dateForReminder); //DO NOT put this code in this same function this is for a quick example only on StackOverflow //otherwise you will have reminders set everytime the users scrolled to a different time //set event reminder //make sure to import EventKit framework EKEventStore *eventDB = [[[EKEventStore alloc] init]autorelease]; EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB]; NSString * eventTitle = [NSString stringWithFormat:@"%@ - %@",app.dealerBusinessName,serviceOrComments.text]; myEvent.title = eventTitle; //double check date one more time NSLog(@"value: %@",destinationDate); //set event time frame (1 hour) the "initWithTimeInterval" is where we account for the users timezone by adding the correctedTime from GMT to the calendar time ( so its not off by hours when entering into calendar) myEvent.startDate = [[[NSDate alloc] initWithTimeInterval:correctedTimeForCalendarEvent sinceDate:destinationDate ]autorelease]; myEvent.endDate = [[[NSDate alloc] initWithTimeInterval:3600 sinceDate:myEvent.startDate]autorelease]; myEvent.allDay = NO; //set event reminders 1 day and 1 hour before myAlarmsArray = [[[NSMutableArray alloc] init] autorelease]; EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-3600]; // 1 Hour EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-86400]; // 1 Day [myAlarmsArray addObject:alarm1]; [myAlarmsArray addObject:alarm2]; myEvent.alarms = myAlarmsArray; [myEvent setCalendar:[eventDB defaultCalendarForNewEvents]]; NSError *err; [eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err]; if (err == noErr) { //no error, but do not show alert because we do that below. } } 
+5
source share

Your data and date formats are omitted by the TimeZone specifier. So something like this:

 [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ssZ"]; 

Will work - Z is a time zone specifier and will analyze both digital offsets and time zone codes. Although in your case, when your input date does not have information about TimeZone, it will not work.

Your correct time line should look like "2011-04-12 19:23:39 -0400" or "2011-04-12 19:23:39 EST"

Depending on where you get your date from, you have to fix it to create a fully qualified date, if you cannot do this, you will have to coordinate the time zone offsets with the server or just the β€œhard code” timezone offset and add this amount seconds to NSDate.

+1
source share

The date is recorded as the UTC date, which can be seen at the end of +0000 . The date format you use to parse the string assumes your local time zone, which is supposedly 4 hours below UTC with summer savings and a standard of -5 hours.

+1
source share

Use -[NSDateFormatter setTimeZone:] to provide a date format with time zone information. You can use a local time zone or if you have a fixed time zone associated with date information, I recommend creating a time zone with a name (for example, "America / East"), and not an abbreviation (for example, "EST" or "EDT ") because the name does not bring daylight saving effects into effect, but uses the correct daylight offset for that date in this time zone.

+1
source share

All Articles