Still having NSDateFormatter result issues even with NSTimezone properly set, why? - iphone

The result is still a day before, I'm just asking myself why, because the NSTimeZone is properly set and is the right one for my country (italy, rome)
here's my stub of code, any ideas?
NSString *dateString = #"03/07/2008";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setFormatterBehavior:[NSDateFormatter defaultFormatterBehavior]];
[formatter setDateFormat:#"dd/MM/yyyy"];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *dateFromString = [formatter dateFromString:dateString];
[formatter release];
the result in dateFromString is this 2008-07-02 22:00:00 +0000.
I've looked for other solutions but the common answer was to set the timezone correctly, in my case it is set properly but the problem still remains.

That is correct because by default the NSDate will return a UTC date in its description +0000. You are a couple of hours ahead of UTC so you get 22:00:00 for the day prior. I am -5 and my result UTC is 2008-07-03 04:00:00 +0000 (DST). The date is correct, it is just being displayed in UTC, if you are trying to display it correctly somewhere just use the date formatter to get a string again.
...
NSDate *dateFromString = [formatter dateFromString:dateString];
NSString *stringFromDate = [formatter stringFromDate:dateFromString];
[formatter release];
NSLog(#"%# : %#", dateFromString, stringFromDate);
2008-07-03 04:00:00 +0000 : 03/07/2008

Related

NSDate from NSString adjusted to device timzone

I have an NSString like this :
NSString *playTime = #"20:45";// GMT
I need to convert this string to NSDate (only hours and seconds) then adjusted to device timezone.
Example:
If user device timezone is set to GMT, I need a result like this : #"20:45";
If user device timezone is set to GMT + 2, I need a result like this : #"22:45";
Try this
NSString *gmtDateString = #"20:45";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"HH:mm"];
//Now you have date in GMT time zone by default dateFormatter timezone would be in local time zone
NSDate *date = [dateFormatter dateFromString:dateString];
//set to local time zone
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
//Now you have string in localDate format
NSString *localDateString = [dateFormatter stringFromDate:date];
The below code will calculate the time w.r.t. local time zone.
Suppose my time zone is GMT+05:30 so the time 05:30 will become 00:00.
NSString *playTime = #"20:45";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"HH:mm";
NSDate *date = [formatter dateFromString:playTime];
NSLog(#"%#",date);
Log is: 2000-01-01 15:15:00 +0000
Use this code for date conversion as per your requirement.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:#"HH:mm"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
logDate = [dateFormat dateFromString:#"20:45"];
NSLog(#"Date %#",[dateFormat stringFromDate:logDate]);

NSDate and dateString in Objective-C

I want to save a date object to the backend of my App. This is the code:
NSDate *date = self.birthPickerView.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *stringFromDate = [formatter stringFromDate:date];
NSLog(#"%#", stringFromDate);
NSDate *endDate = [formatter dateFromString:stringFromDate];
[formatter release];
NSLog(#"%#", endDate);
// Save to database
[user setObject:endDate forKey:#"birth"];
This is the print out result:
1985-03-05 00:00:00
1985-03-04 23:00:00 +0000
The end date is not right. I want to save 1985-03-05 in the database. Can you help me what is wrong?
Edit
[self.birthPickerView setTimeZone:[NSTimeZone localTimeZone]];
NSLog(#"%#", self.birthPickerView.date);
NSLog(#"%#", self.birthPickerView.timeZone);
[user setObject:self.birthPickerView.date forKey:self.navTitle];
This code save 1984-03-04 23:00:00 in database. what is wrong with it?
I don't see why would you need to convert your date into a string, and then back into the date again.
However, I'm pretty sure this happens because you didn't set the timezone on your NSDateFormatter. The default time zone is GMT which might cause the time offset you see there.

How to get my time in UTC for iPhone project?

How to get my country time in UTC for iPhone development?
Here is the Code, Just Call below method when you want to set TimeZone and Date Format.
-(void)setDateFormat
{
NsDate myDate = [NSDate date];//here it returns current date of device.
//now set the timeZone and set the Date format to this date as you want.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *newDate = [dateFormatter stringFromDate:myDate];
// here you have new Date with desired format and TimeZone.
}
-(NSString *)UTCFormDate:(NSDate *)myDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate:myDate];
return dateString;
}
Source:
https://stackoverflow.com/a/2615847/857865
[NSDate date] gives you the current point in time, which may be printed in UTC, GMT, EST or any other timezone. (Representation-wise, the current point in time is the number of seconds since a reference date in UTC.)
To get the current point in time in UTC as a string, get an NSDateFormatter and configure it to output a timestamp using the UTC timezone.

nsdateformatter timezone issues

"2012-01-30 00:00:00 +0000",
"2012-01-31 00:00:00 +0000",
"2012-02-01 00:00:00 +0000"
I have a list of dates from xml listed above .i need to format and sort..i am using the code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMM dd"];
[dateFormatter stringFromDate:d];
I am getting(jan 30,jan 31,feb 01)(india) and client getting in US location(jan 29,jan 30,jan 31)..
After that i update something and i set timezone..
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"America/Los_Angeles"]];
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]
autorelease];
i am getting wrong date(jan 29,jan 30,jan 31)....and i am searching some other sites and i am setting
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
This code works for me .i don't know this code works for other countries
I need correct date depends on countries...
Set the timezone for the date you parse. The formatter assumes GMT and the date you print assumes your local timezone.
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];

Problem in setting nsdate value from a string

NSDate *My_StartDate,*My_EndDate ;
NSDateFormatter * df= [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
My_StartDate = [df dateFromString:#"01/05/2010 10:15:33"];
My_EndDate = [df dateFromString:#"01/05/2010 10:45:33"];
NSLog(#"%#",My_StartDate);
NSLog(#"%#",My_EndDate);
In the log i get something like this for the my_startdate as 2010-05-01 04:45:33 +0000 and end date as 2010-05-01 05:15:33 +0000 instead i should have got value as for start date as 2010-05-01 10:15:33 +0000 and end date as 2010-05-01 10:45:33 +0000
Try with below function:
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm:ss"];
//[timeFormat setDateFormat:#"HH:mm"];
//[timeFormat setDateFormat:#"HH:mm a"];
////
NSString *theDate = [dateFormat stringFromDate:dateValue];
NSString *theTime = [timeFormat stringFromDate:dateValue];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
return theDate;
}
Change Format of data as per your need.
Let me know in case of any difficulty.
Cheers.
This shows date which follow American standard time string but by this reason you don't get any problem in making your logic.Also
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
this format using 12 hour format (means 2:03 pm and 2:03 am) and date object never use am and pm for showing date object value but when you convert it correctly then it gives you right date and time.
If you feel you get any problem then use different locale for that.
It is displaying asper the GMT+4.30 time.It displays like that only.When you are converting that date to string using the DateFormatter it gives the same date(Whichever you want like start date as 01/05/2010 10:15:33 and end date as 01/05/2010 10:45:33).
NSDateFormatter * dateformatter= [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSString *dat = [dateformatter stringfromDate:My_StartDate];
then you will get the output as 01/05/2010 10:15:33
You might want to set the time zone of the date formatter to GMT here. Do it using
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
before you do dateFromString: calls. This will give you what you want.
Just need to update here in your code:
I might be like that your time would be in 24 hours format, so at that time you need to use this ....other than that you need to set the timezone.
Follow this link for All zone : http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
to
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
You are Done ;)