ios – 将NSDate从一个时区更改为另一个时间区域

ios – 将NSDate从一个时区更改为另一个时间区域,第1张

概述在NSString中有一个像“2012-12-17 04:36:25”(即GMT)的日期,可以简单地将其更改为其他时区,如EST,CST 我目前看到的所有步骤都采取了许多不必要的步骤 NSString *str = @"2012-12-17 04:36:25";NSDateFormatter* gmtDf = [[[NSDateFormatter alloc] init] autorelease 在Nsstring中有一个像“2012-12-17 04:36:25”(即GMT)的日期,可以简单地将其更改为其他时区,如EST,CST

我目前看到的所有步骤都采取了许多不必要的步骤

解决方法
Nsstring *str = @"2012-12-17 04:36:25";NSDateFormatter* gmtDf = [[[NSDateFormatter alloc] init] autorelease];[gmtDf setTimeZone:[NSTimeZone timeZoneWithname:@"GMT"]];[gmtDf setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSDate* gmtDate = [gmtDf dateFromString:str];NSLog(@"%@",gmtDate);NSDateFormatter* estDf = [[[NSDateFormatter alloc] init] autorelease];[estDf setTimeZone:[NSTimeZone timeZoneWithname:@"EST"]];[estDf setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSDate *estDate = [estDf dateFromString:[gmtDf stringFromDate:gmtDate]]; // you can also use strNSLog(@"%@",estDate);

编辑:添加快捷代码

let str: String = "2012-12-17 04:36:25"let gmtDf: NSDateFormatter = NSDateFormatter()gmtDf.timeZone = NSTimeZone(name: "GMT")gmtDf.dateFormat = "yyyy-MM-dd HH:mm:ss"let gmtDate: NSDate = gmtDf.dateFromString(str)!print(gmtDate)let estDf: NSDateFormatter = NSDateFormatter()estDf.timeZone = NSTimeZone(name: "EST")estDf.dateFormat = "yyyy-MM-dd HH:mm:ss"let estDate: NSDate = estDf.dateFromString(gmtDf.stringFromDate(gmtDate))!print(estDate)
总结

以上是内存溢出为你收集整理的ios – 将NSDate从一个时区更改为另一个时间区域全部内容,希望文章能够帮你解决ios – 将NSDate从一个时区更改为另一个时间区域所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1112526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存