My current Device time is --- 2013-05-09 10:23 AM
I convert it int GMT with following code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"time in GMT ------ %#",timeStamp);
And I got the out put as
time in GMT ------ 2013-05-10 04:53 AM
then I found my time zone
NSTimeZone *localTime = [NSTimeZone systemTimeZone];
NSLog(#"local timezone is------ %#",[localTime name]);
out put
local timezone is------ Asia/Kolkata
Now I need to convert the above GMT time to local time.I used the following code
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:[localTime name]];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"local time is ------- %#",dateFromString);
But I am getting a wrong out put..
local time is ------- 2013-05-09 19:23:00 +0000
Why this happen ?
I should get local time as - 2013-05-09 10:23 AM
Your timestamp string is in GMT. This means that when you want to convert the GMT string back to an NSDate, your date formatter needs to be set to GMT, not your local timezone. By doing this you will get the proper NSDate.
And don't forget that when you log an NSDate object, it will display in GMT regardless of anything else.
// get current date/time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(#"User's current time in their preference format:%#",currentTime);
Please my dear do little effort on google means first search your question on google and if you don't get your answer in that case put your question on stackover flow.
And see your answer on below link....
iPhone: NSDate convert GMT to local time
iphone time conversion gmt to local time
Date/time conversion to user's local time - issue
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"time in GMT ------ %#",timeStamp);
NSTimeZone *localTime = [NSTimeZone systemTimeZone];
NSLog(#"local timezone is------ %#",[localTime name]);
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"local time is ------- %#",dateFromString);
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"yyyy-MM-dd HH:mm";
NSTimeZone *gmt1 = [NSTimeZone localTimeZone];
[dateFormatter1 setTimeZone:gmt1];
NSString *timeStamp1 = [dateFormatter1 stringFromDate:[NSDate date]];
NSLog(#"local time is ------- %#",timeStamp1);
Output:
time in GMT ------ 2013-05-10 05:13 AM
local timezone is------ Asia/Kolkata
local time is ------- 2013-05-10 00:13:00 +0000
local time is ------- 2013-05-10 10:43
Try Using This Code :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm a"];
// get time zone
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *date = [NSDate date];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
date = [dateFormatter dateFromString:timeStamp];
// change time zone
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
// or you can use SystemTimeZone
// [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *strDateWithLocalTimeZone = [dateFormatter stringFromDate:date];
NSLog(#"Local Time Zone Date %#", strDateWithLocalTimeZone);
Related
I got current GMT time - 2013-05-15 08:55 AM
my current local time is - 2013-05-15 02:06 PM
and time zone is - Asia/Kolkata
I tried to convert the above GMT to my local time with this code
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
// timeStamp - is the above GMT time
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"local time is %#",dateFromString);
but this give me wron time as - 2013-05-14 19:04:00 +0000 instead of 2013-05-14 02:06 PM
Why this happens?
please help me to clear this.Thanks in advance
Nothing is going wrong, a NSDate instance will never have a timezone ( well it will but it is always GMT, indicated by the +0000 timezone in the date description).
What you are telling dateformatter in your code is that the timezone of the date you want to parse is Asia/Kolkata but you want it to be GMT.
First you need to make a NSDate object from the input date string with the time zone set to the correct time zone, GMT as you indicated.
NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(#"Input date as GMT: %#",dateFromString);
Then when you present the date as an string use :
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
NSLog(#"Date formated with local time zone: %#",dateRepresentation);
The shortest way that I found to convert GMT time to your local timezone is
NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:dateInGMT];
Furthermore, to convert this date value into string
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];//use the formatted as per your requirement.
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *localTimeDateString = [formatter stringFromDate:localDateTime];
NSLog(#"local time is %#",[dateFormatter stringFromDate: dateFromString]);
NSTimeZone* gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"Asia/Kolkata"];
NSDateFormatter *gmtDateFormatter = [[NSDateFormatter alloc] init];
[gmtDateFormatter setTimeZone:gmtTimeZone];
NSDate *date = [gmtDateFormatter dateFromString:timestamp];
NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:localTimeZone];
NSLog(#"local time is %#",[localDateFormatter stringFromDate:date]);
Use timeZoneWithName
NSDate *date=[[NSDate alloc]init];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
[timeFormat setDateFormat:#"HH:mm"];
NSString *dateStr=[timeFormat stringFromDate:date];
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]);
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.
I have a string with time in GMT and i want to make it according to the system time zone but its not working properly -
NSLog(#"Time Str = %#",Time);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy hh:mm a"];
[NSTimeZone resetSystemTimeZone];
NSLog(#"system time zone = %#",[NSTimeZone systemTimeZone]);
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormat dateFromString:Time];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"date from string = %#",date);
NSLog(#"string from date = %#",[dateFormat stringFromDate:date]);
Output at console -
////////////////////////////////////////////////////////////////////////////////////////////////
Time Str = 09-12-2011 07:57 AM
system time zone = Asia/Calcutta (IST) offset 19800
date from string = 2011-12-09 02:27:00 +0000
string from date = 09-12-2011 07:57 AM
////////////////////////////////////////////////////////////////////////////////////////////////
Calcutta is +5:30 from GMT, so the time in date should be 1:27 but its showing 02:27. Also when i take a string from this date its showing me the same sting that i used to make date, i want the string updated according system time zone.
Thanks
If the date string is in GMT you can't use your system Timezone to create the NSDate from the NSString.
replace the first occurrence of [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
with [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
I have the following date:
2011-10-20T01:10:50Z
I would like it to be formatted to
"M/d/yy 'at' h:mma"
Here is my code:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-dd-MM'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:#"created_at"]];
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
[dateFormatter release];
NSLog(#"%#", finalTime);
Unfortunately the finalTime is NULL here.
You have dd-MM backwards. You have 10 for dd and 20 MM. There is no month 20.
2011-10-20T01:10:50Z
#"yyyy-dd-MM'T'HH:mm:ss'Z'"
Because of that, the newTime was null and the finalTime was null.
Here's with the fix. This:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:#"2011-10-20T01:10:50Z"];
NSLog(#"original time: %#", newTime);
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
NSLog(#"%#", finalTime);
[dateFormatter release];
Outputs:
2011-10-19 22:05:15.107 Craplet[4231:707] original time: 2011-10-20 01:10:50 +0000
2011-10-19 22:05:15.116 Craplet[4231:707] 10/19/11 at 9:10PM
This technical note helped me resolve the same issue: https://developer.apple.com/library/ios/qa/qa1480/_index.html
In a nutshell, you need to set 3 things on NSDateFormatter:
Locale
TimeZone
DateFormat
code sample:
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:#"2014-10-23T01:10:50.000Z"];
I was using NSDateFormatter to successfully parse UTC strings to NSDate by only setting time zone and date format. When it suddenly stopped working I found this tech note and added setLocal: which resolved the issue for me.
Because Swift:
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
let dateHopefully = dateFormatter.dateFromString("2016-07-29T17:15:02Z")
dateFormatter.timeZone = NSTimeZone.systemTimeZone()
dateFormatter.dateFormat = "M/d/yy 'at' h:mma"
let lastTime = dateFormatter.stringFromDate(dateHopefully!)
print("Here is the hopeful date \(dateHopefully) and here is the lastTime \(lastTime)")