Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert NSDate into UTC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 537
    Comment on it

    Coordinated Universal Time (UTC) is the standard time zone. All time zones are at some time ahead or behind UTC. If there is a need to convert timeZones of different countries then we can first change the local time to UTC and then to the desired format. So UTC makes the conversion easier.

     

    The following code will convert the date into UTC format.

    In viewDidLoad method implement the below lines of code :-

     NSDate *date = [NSDate date];  // get the current date
    [self getUTCFormateDate:date];
    

     

    Now define the  getUTCFormatDate method as :-

    -(NSString *)getUTCFormat:(NSDate *)localDate
    {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
        NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; 
     
        [dateFormatter setTimeZone:timeZone];
    
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:a"];  // set the format in which we want to show the date
    
        NSString *dateString = [dateFormatter stringFromDate:localDate];
    
        [_lblDisplayDate setText:dateString];  // set the dateString to the _lblDisplayDate 
    
        return dateString;
        }

     

    The output will be as shown below :-

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: