开发者

Convert date in to UTC Format

开发者 https://www.devze.com 2023-03-02 18:54 出处:网络
I want to convert the local date in UTC format.(US Format开发者_Python百科)-(NSString *)getUTCFormateDate:(NSDate *)localDate

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

精彩评论

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