Reputation: 639
This is really odd, the code below takes self.danceTimeIn (its text state) and converts it to an actual time. The problem is that its coming up 1 hour LESS than what's entered. Meaning that if I enter 14:03 I'll get 13:03 in the database! The same thing is happening with the date version of this code
++++++++++++++++++++++
TIME
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:@"HH:mm"];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
DATE
NSString *danceDateValue = self.danceDate.text;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yy"];
NSDate *date = [dateFormat dateFromString: danceDateValue];
++++++++++++++++++++++
Anyone ???
Upvotes: 0
Views: 100
Reputation: 15015
It depends on the timezone:-
first Check your local time zone
NSTimeZone *tz=[NSTimeZone timeZoneWithName:@"GMT"];
[dateformat setTimeZone:tz];
and then set your date accordingly.
Upvotes: 1
Reputation: 1071
Time zone may be causing this problem.Try
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
May be this will help.
Upvotes: 1
Reputation: 6954
Try using this. NSDateFormater change date according to locale of your device settings, if you set locale properlyl, you will get proper date. Try if this works :)
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:@"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[timeFormatIn setLocale:usLocale];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
Upvotes: 1