Miles Works
Miles Works

Reputation: 639

date AND time being entered is 1 hr or day off under iOS/Objective C

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

Answers (3)

Hussain Shabbir
Hussain Shabbir

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

iYousafzai
iYousafzai

Reputation: 1071

Time zone may be causing this problem.Try

[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

May be this will help.

Upvotes: 1

DivineDesert
DivineDesert

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

Related Questions