How to get my country time in UTC for iPhone development?
Asked
Active
Viewed 2,837 times
2
-
1you can use NSDateFormatter for this set timezone to "UTC" convert your date in UTC – priyanka Nov 12 '12 at 11:28
-
sorry,but can you add the code here,because i tried your suggestion but i didn't have correct time – Apple Developer Nov 12 '12 at 11:41
-
Check and make sure the time zone is correctly set on your iOS device or computer. I believe Apple will handle the daylight saving time for you as long as the time zone is correctly set. You might be getting this error if you have the time incorrectly set for the configured time zone. – chris Nov 12 '12 at 15:47
3 Answers
1
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.
}

Community
- 1
- 1

Kamar Shad
- 6,089
- 1
- 29
- 56
-
i tried this code,but the time offset is wrong, because my country has stopped the dayNight save time for this year..any idea about how to solve this problem? – Apple Developer Nov 12 '12 at 12:33
0
-(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;
}
-
Thank you very much,this code returns the time +2 while my country is +3,do you have any idea why? – Apple Developer Nov 12 '12 at 11:40
-
-
-
mmm, can we get the country UTC directly without using the device time? – Apple Developer Nov 12 '12 at 11:46
0
[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.

Jesper
- 7,477
- 4
- 40
- 57
-
Yes, i tried this but it doesn't give me the correct time for my country,can you please submit a code for this? – Apple Developer Nov 12 '12 at 11:43
-
Your question doesn't say exactly what you want to achieve so I can't show you exactly how to achieve it. Please provide an example of what you want to achieve. – Jesper Nov 12 '12 at 11:46
-
Ok, sorry for this, my main goal is to have my country time in UTC, the code in this link http://stackoverflow.com/a/2615847/857865 is helpful, but my country has stopped the daylight saving time,so the returned result is after the UTC time by one hour.. do you know any way to get the UTC time for my country directly without having the device time? appreciate your help – Apple Developer Nov 12 '12 at 11:50
-
There's no such time as "UTC time for your country". Maybe you mean the UTC offset, how many hours ahead or behind UTC your time zone is. Don't try to manually adjust this by adding or removing hours to the timestamp or to the offset since the device will already know what time zone you're in. Just [format the date](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html) and be sure to include a time zone offset marker - "Z" - in the format string. – Jesper Nov 12 '12 at 12:09