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.
Related
in my iPhone application I need to convert NSString to NSDate with formaT. Here is my code:
+(NSDate*)dateFromJsonString:(NSString*)string{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
NSLog(#"%#", [dateFormat dateFromString:string]);
return [dateFormat dateFromString:string];
}
If parameter "string" is 2013-05-30T15:53:02 after converting - [dateFormat dateFromString:string] it becomes 2013-05-30 12:53:02 +0000 , so there is a difference in 3 hours. How can I fix it?
You need to set the correct time zone. Here is a sample, correct it to the time zone of your server:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
return [dateFormatter dateFromString:dict[#"completed_at"]];
EDIT: You can check the available time zones with a class method on NSTimeZone:
+ (NSArray *)knownTimeZoneNames
Just output it to the console, and choose the time zone that corresponds to the server one.
EDIT 2: Probably this is more useful:
+ (NSDictionary *)abbreviationDictionary
Choose your time zone and than just change the #"UTC" in the code above with the desired abbreviation.
Try this dateFormat. Since timezone is not given in dateString, dateFormatter assumes it to be local timezone. If you know the timezone set it to formatter.
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
Check this answer for more details.
This is TimeZone issue so use this code to fix this problem :
+(NSDate*)dateFromJsonString:(NSString*)string
{
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"%#", [dateFormat dateFromString:string]);
return [dateFormat dateFromString:string];
}
Hope it helps you.
You can use the following code to set timezone.
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
But before you try that, i should tell you that if you are logging the date, it will show it in UTC format and not in the timezone of your computer. So dont check your date by printing it. So try converting the date to string and printing it to check if the date is correct or not.
Try logging NSLog(#"%#",[dateFormatter stringFromDate:date]); first
You just need to set current time zone for date formatter. Because of it, you got some time difference. Use below code..
+(NSDate*)dateFromJsonString:(NSString*)string
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[inputFormatter setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss"];
NSDate *xExpDate = [inputFormatter dateFromString:string];
return xExpDate;
}
When i am
[dateFormatter setDateFormat:#"HH:mm"]
using to fetch the current hour
i am getting correct time till 12:59 but after that it again starts from 1:00 i.e. 12:59 is displayed as 12:59 but 14:00 is displyed as 2:00.
Please let me if anyone is also having a similar problem and suggest a solution.
Thanks
Please check the code below.
NSDateFormatter *dateFormator = [[NSDateFormatter alloc] init];
dateFormator.dateStyle = NSDateFormatterMediumStyle;
dateFormator.dateFormat=#"HH:mm:ss";
NSCalendar * calender=[[NSCalendar alloc]initWithCalendarIdentifier:[NSGregorianCalendar autorelease]];
NSDateComponents * component=[calender components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:date];
I agree with
[dateFormatter setDateFormat:#"HH:mm"]
should give you the time in 24 Hours. But this doesn't work if the user set 24 hour to off. Despite using HH in a format string, the user setting overrides this. This might be the problem in your case!
Use this one
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd kk:mm:ss"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormatter stringFromDate:date];
You can use
[dateFormatter setDateFormat:#"H:mm"]
for 24 hour time format
Set your dateformat to
[dateFormatter setDateformat:#"HH:MM a"];
Try this code
[dateFormatter setDateFormat:#"hh:mm:ss"];
HH : 24 hour format
hh : 12 hour format
So in your case
//after conversion to date
if ON
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
if OFF
[formatter setDateFormat:#"YYYY-MM-dd hh:mm:ss a"];
Hope it works for you.
I am working on writing iPhone app.
I want to get current date as following format.
Tur, 18 Apr 2013 09:20:47
I tried with following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"E, dd MMM yyyy HH:mm:ss"];
NSString *formattedDate = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
The problem is my iPhone region is not US, so I get following string.
木, 18 4月 2013 09:20:47
How can I resolve this?
Set up the locale property of date to get it in US format
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]
autorelease];
or
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
I have't use NSDateFormatter from a long time but i am proving some links -
For better understanding you can refer -
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html and
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
I hope, it might help you.
Currently i am working in iPhone application, Here i have using NSString to store (2012-07-11 11:49:55 +0000) and using NSDateFormatter to convert the date and time but the output comes 05:19:55, but i want 11:49:55, please any one help me
Here i tried the source code for your reference
NSString *dateString = [NSString stringwithformat:%#,(2012-07-11 11:49:55 +0000)];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"The Current Time is %#",[dateFormatter stringFromDate:dateString]);
[dateFormatter release];
Your system time zone is not +0000. So the time is formatted according to it. The solution is to set same time zone to GMT (+0000).
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
The reason is you are changing the TimeZone, please comment out the following line and it should give back the time you provided in the dateString:
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
"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"]];