nsdateformatter timezone issues - iphone

"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"]];

Related

How to get timezone in GMT Form in iOS6

In my iPhone app I want to get system timezone in GMT. Am Using the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"zzz";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSString *timeZoneString=[timeStamp substringFromIndex:3];
[dateFormatter release];
Am getting timezone in GMT form in iOS5 but not in iOS6. How to get timezone in GMT form in iOS6? Please Help!!!
You can check with,
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
in place of,
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
I have revised #ACB's code as there were a few typos , try [dateFormatter setTimezone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; instead of [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];. This seems to work for me.
I would like to give credit for this answer to #ACB. He/she came up with this technique and I only confirmed it.

How to get yyyy-mm-dd hh:mm:ss format from 28 Jul 2011 22:33:57 in iPhone?

I have stuck in issue in which i have to convert date format is Thu, 28 Jul 2011 22:33:57 +0000 into yyyy-mm-dd hh:mm:ss
please give me some idea how to do this?
What you want is to use the NSDateFormatter. Something like this:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString* dateString = [f stringFromDate:date];
Do note that the hours will still follow the users selected locale. Use kk:mm:ss to enforce a 24-hour time.
what have you tried? it's difficult to answer questions like this...
first you have to parse the date into an NSDate, use an NSDateFormatter, the incoming format looks like POSIX date format so should be easy.
then you want to output to the format you specify with another NSDateFormatter
You need to use an NSDateformatter to convert the first date to a string with the following syntax.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]]; //Put whatever date you want to convert
Then if you want the date as an NSDate and you have the string generated above just put the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSDate * date = [dateFormatter dateFromString: dateString]; //String generated above

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 ;)

dateFromString returning wrongDate

I am converting an NSString like #"12:25 AM May 27 2011" into NSDate.But as I convert it using the following code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a MMM dd YYYY"];
NSDate *date = [[dateFormatter dateFromString:#"12:25 AM May 27 2011"] copy];
[dateFormatter release];
I get the wrong Date as 2010-12-25 19:25:00 +0000
Can anyone please help me around here
It's not a wrong date, the time is displayed GMT hance (+0000) if you set the correct timezone very thing will be oke.

How can I get the date but not time from NSDate

I need the date as a string but not the time and it has to be localized.
So for example USA should be Sep 25 2009 but for New Zealand it would be 25 Sep 2009.
I can get the date into a string by specifying the format "MMM dd YYYY" but It's not localized.
Any ideas?
[NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterMediumStyle
timeStyle:0]
The key is to send setTimeStyle:kCFDateFormatterNoStyle to the dateFormatter. That sets the dateFormatter so that it will only format the date and not output the time:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [NSLocale currentLocale]];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle]; // or whichever style...
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle];
NSString* dateString = [dateFormatter stringFromDate: [NSDate date]];
This gives me the following output:
12/18/09