V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
wohenyingyu02
V2EX  ›  iDev

NSDate 转化成 NSString 再转回来在某些机子上总有一小时的误差

  •  
  •   wohenyingyu02 · 2017-06-12 14:07:02 +08:00 · 3592 次点击
    这是一个创建于 2482 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目的是吧 NSDate 转换成 NSString 再变成 UTF8 的 char *存入 sqlite,用的时候再转换回 NSDate。

    代码:

    #define DATE_FORMATTER_STRING @"MM/dd/yyyy hh:mm:ss a Z"

    //从 NSString 提取之前存入的时间

    • (NSDate*_Nonnull)stringDateFromString:(NSString*_Nonnull)string { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:DATE_FORMATTER_STRING]; return [dateFormatter dateFromString:string]; }

    //把 NSDate 转化成 NSString

    • (NSString*_Nonnull)stringDateFromDate:(NSDate*_Nonnull)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:DATE_FORMATTER_STRING]; return [dateFormatter stringFromDate:date]; } ’‘’

    输出日期 NSString: Output date string: 06/12/2017 +0800 13:42:32 下午 +0800 输出转换回来的 NSDate: Output date: Mon Jun 12 12:42:32 2017

    时区也带了,而且只是在某些机子上才会出现,不多不少就是差 1 小时,差 8 小时我可以理解,1 小时我有点理解不了了,难道是夏令时冬令时影响?

    8 条回复    2017-06-12 21:29:07 +08:00
    jiangdaohong
        1
    jiangdaohong  
       2017-06-12 14:17:46 +08:00
    数据库直接存 DATETIME 或者转成时间戳进行存储啊
    Valyrian
        2
    Valyrian  
       2017-06-12 14:19:05 +08:00
    存 unix timestamp 好点
    wohenyingyu02
        3
    wohenyingyu02  
    OP
       2017-06-12 14:33:19 +08:00
    @jiangdaohong
    @Valyrian
    确实,但是还是想问问这样为啥会差时间……
    Valyrian
        4
    Valyrian  
       2017-06-12 15:52:50 +08:00 via iPad
    paradoxs
        5
    paradoxs  
       2017-06-12 15:54:05 +08:00
    NSDate 要指定时区
    HuxleyYang
        6
    HuxleyYang  
       2017-06-12 19:33:49 +08:00
    一般都存 NSTimeInterval 比较好。你这个情况可能和夏令时( Daylight Saving Time )有关
    geelaw
        7
    geelaw  
       2017-06-12 19:45:32 +08:00 via iPhone
    还可能是半小时、一个半小时之类的

    一个很好的做法是内部总是使用世界时,显示的时候再转换为本地时间。
    wohenyingyu02
        8
    wohenyingyu02  
    OP
       2017-06-12 21:29:07 +08:00
    @HuxleyYang 有可能,谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2656 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:20 · PVG 23:20 · LAX 08:20 · JFK 11:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.