How to get my time in UTC for iPhone project? - iphone

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.

Related

Calculate date and time corresponding to different timezone

I have an ipad application that draws a graph with time on y-axis. Now my problem is that, this same graph by a user can be accessed anywhere in the world. But then the time on the graph should be adjusted so that it should correspond to the current timezone of the user. So the time on the graph should compensate for any difference in time between timezones. Can anyone tell me how to take the timezone from current device and calculate the offset time if i am passing the timezone of the device from which the graph is sent.
This is my date formatting function. I intend to pass the timezone also along with the date.
+(NSDate *)DateUKFormatFromString:(NSString *)date
{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate* returnDate = [dateFormatter dateFromString:date];
return returnDate;
}
Thanks in advance.
there is a dateFormatter instance method setTimeZone: use it like this
[NSTimeZone resetSystemTimeZone];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
For information on the two methods, please read documentation. Hope it helps. Cheers
Edit:
If date coming from server is for a specific time Zone (time zone of the server). Then you will need to get the timeZone
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:"Server timezone name"];
NSInteger serverDifference = [timeZone secondsFromGMT];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *systemTimeZone = [NSTimeZone systemTimeZone];
NSInteger systemDifference = [systemTimeZone secondsFromGMT];
NSInteger difference = systemDifference - serverDifference;
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:difference]];
try this in case you are not getting a 0 GMT difference time from server.
+(NSDate *)DateUKFormatMonthFromString:(NSString *)date AndTimeZoneName:(NSString *) timeZoneName
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneName];
[formatter setTimeZone:timeZone];
NSDate *date1 = [formatter dateFromString:date];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
return date1;
}
In the end, this worked for me... Seems that iOS will take care of time zone differences on its own...!!!

Issue in coverting GMT time to Local time

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

Convert GMT time to local time

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

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

NSDateFormatter and Time Zone issue?

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