gpt4 book ai didi

ios - 从字符串中获取日期

转载 作者:行者123 更新时间:2023-11-30 14:08:41 25 4
gpt4 key购买 nike

我正在处理日期,并且我在从字符串中获取日期时遇到问题,我在这里简化了我的问题:

let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-YYYY"
formatter.stringFromDate(date)
formatter.dateFromString(formatter.stringFromDate(date))

输出是:“25-05-2015”(这很好)“2014 年 12 月 21 日凌晨 12:00”(???)

最佳答案

问题是 Y 代表 weekOfYear。您必须使用“dd-MM-yyyy”。顺便说一句,别忘了set your date formatter locale to "en_US_POSIX" .

If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences. "en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iOS as it does on OS X, and as it it does on other platforms).

关于ios - 从字符串中获取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com