Apple Developer
Apple Developer

Reputation: 99

How to get my time in UTC for iPhone project?

How to get my country time in UTC for iPhone development?

Upvotes: 2

Views: 2911

Answers (3)

Mohammad Kamar Shad
Mohammad Kamar Shad

Reputation: 6067

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.
}

Upvotes: 1

Jesper
Jesper

Reputation: 7605

[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.

Upvotes: 0

Kaz
Kaz

Reputation: 1466

-(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

Upvotes: 0

Related Questions