Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-03-2010, 03:27 AM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 40
TSurridge is on a distinguished road
Default NSDateFormatter timezone issue. UIDatePicker tz is -0500 & NSDateFormatter is -0600?

I am not sure what I am missing here but it has been driving me nuts for the past few hours trying to figure this out.

I am trying to save the date/time from a UIDatePicker into an SQLite database using UTC format.

So I use the UIDatePicker and shows the time for: 2009-11-15T00:30 as 7:30pm. (-0500 timezone what it is defaulting to.)

However, my issue is later. I access this data and use dateFromString and the time is off by an hour. For whatever reason it no longer uses the -0500 time zone it used with the UIDatePicker to convert the UTC time but now uses -0600 timezone. I have No idea what has changed to make it -0600 vs the -0500.


Code:
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
	[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation: @"UTC"]];
	NSDate *sqlDateFormatted = [[NSDate alloc] init];
	sqlDateFormatted = [dateFormatter dateFromString:savedSqlDate];
	NSLog(@"savedSqlDate: %@", savedSqlDate);
	NSLog(@"sqlDateFormatted: %@", sqlDateFormatted);
	[dateFormatter release];

	
	NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	[dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"];
	NSString *dateString = [dateFormat stringFromDate:sqlDateFormatted];
	NSLog(@"dateString: %@",dateString);
	cell.gameDate.text = dateString;
	[dateFormat release];
NSlog info:
savedSqlDate: 2010-01-09T00:30
sqlDateFormatted: 2010-01-08 18:30:00 -0600
dateString: 01/08/2010 06:30PM

Last edited by TSurridge; 08-03-2010 at 04:07 AM.
TSurridge is offline   Reply With Quote
Old 08-03-2010, 04:21 AM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 40
TSurridge is on a distinguished road
Default

Quote:
Originally Posted by TSurridge View Post
I am not sure what I am missing here but it has been driving me nuts for the past few hours trying to figure this out.
Well, all this mess is because of Daylight Savings time. CST and CDT and depending on the date the time is -0500 or -0600 according to DST.

Errrr wish I would have looked more into that HOURS ago!
TSurridge is offline   Reply With Quote
Old 08-09-2010, 06:59 AM   #3 (permalink)
Registered Member
 
fousulameen's Avatar
 
Join Date: Jul 2010
Location: chennai
Posts: 41
fousulameen is on a distinguished road
Send a message via Skype™ to fousulameen
Default

Quote:
Originally Posted by TSurridge View Post
I am not sure what I am missing here but it has been driving me nuts for the past few hours trying to figure this out.

I am trying to save the date/time from a UIDatePicker into an SQLite database using UTC format.

So I use the UIDatePicker and shows the time for: 2009-11-15T00:30 as 7:30pm. (-0500 timezone what it is defaulting to.)

However, my issue is later. I access this data and use dateFromString and the time is off by an hour. For whatever reason it no longer uses the -0500 time zone it used with the UIDatePicker to convert the UTC time but now uses -0600 timezone. I have No idea what has changed to make it -0600 vs the -0500.


Code:
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
	[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation: @"UTC"]];
	NSDate *sqlDateFormatted = [[NSDate alloc] init];
	sqlDateFormatted = [dateFormatter dateFromString:savedSqlDate];
	NSLog(@"savedSqlDate: %@", savedSqlDate);
	NSLog(@"sqlDateFormatted: %@", sqlDateFormatted);
	[dateFormatter release];

	
	NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	[dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"];
	NSString *dateString = [dateFormat stringFromDate:sqlDateFormatted];
	NSLog(@"dateString: %@",dateString);
	cell.gameDate.text = dateString;
	[dateFormat release];
NSlog info:
savedSqlDate: 2010-01-09T00:30
sqlDateFormatted: 2010-01-08 18:30:00 -0600
dateString: 01/08/2010 06:30PM




try this code:

//date from string

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation: @"UTC"]];
NSDate *savedDate = [[NSDate alloc] init];
NSLog(@"savedDate: %@", savedDate);
[dateFormatter release];


//string from date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"];
NSDate *sqlDateFormatted = [[NSDate alloc] init];
NSString *dateString = [dateFormat stringFromDate:sqlDateFormatted];
NSLog(@"dateString: %@",dateString);
[dateFormat release];


fousulameen is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 328
8 members and 320 guests
alexP, gordo26, headkaze, mistergreen2011, nobstudio, rayjeong, Sloshmonster, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:20 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0