开发者

Convert date in to UTC Format

I want to convert the local date in UTC format.(US Format开发者_Python百科)


-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:localDate];
    [dateFormatter release];
    return dateString;
}


You should set the NSTimeZone

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate *date=[dateFormat dateFromString:[NSString stringWithFormat:@"%@",dateString]];
NSLog(@"Date %@",date);


Swift 4.2 version:

func dateAsString(inputDate: Date)->String
{
    let format = DateFormatter.init()
    format.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
    format.timeZone = TimeZone.init(identifier: "UTC")
    let str = format.string(from: inputDate)

    return str
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜