DateFormatter 在 Swift 中为没有时间的特定日期字符串返回 nil [英] DateFormatter's returns nil for specific date strings without time in Swift

查看:27
本文介绍了DateFormatter 在 Swift 中为没有时间的特定日期字符串返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白这段代码发生了什么,我正在尝试将字符串转换为日期.我不明白的是,转换适用于大多数日期,但不适用于仅 2 个日期.

I really don't get what is happening with this code, I'm trying to convert a string to date. What I don't understand is that the conversion works for most of the dates, but doesn't work specially for only 2 dates.

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "es")
dateFormatter.dateFormat = "dd 'de' MMMM"

dateFormatter.dateFromString("1 de octubre")
dateFormatter.dateFromString("2 de octubre")
dateFormatter.dateFromString("3 de octubre")
dateFormatter.dateFromString("4 de octubre")
dateFormatter.dateFromString("5 de octubre")
dateFormatter.dateFromString("6 de octubre")
dateFormatter.dateFromString("7 de octubre")
dateFormatter.dateFromString("8 de octubre")
dateFormatter.dateFromString("9 de octubre")
dateFormatter.dateFromString("10 de octubre")
dateFormatter.dateFromString("11 de octubre")
dateFormatter.dateFromString("12 de octubre")
dateFormatter.dateFromString("13 de octubre")
dateFormatter.dateFromString("14 de octubre")
dateFormatter.dateFromString("15 de octubre")
dateFormatter.dateFromString("16 de octubre")
dateFormatter.dateFromString("17 de octubre")
dateFormatter.dateFromString("18 de octubre")
dateFormatter.dateFromString("19 de octubre")
dateFormatter.dateFromString("20 de octubre")
dateFormatter.dateFromString("21 de octubre")
dateFormatter.dateFromString("22 de octubre")
dateFormatter.dateFromString("23 de octubre")
dateFormatter.dateFromString("24 de octubre")
dateFormatter.dateFromString("25 de octubre")
dateFormatter.dateFromString("26 de octubre")
dateFormatter.dateFromString("27 de octubre")
dateFormatter.dateFromString("28 de octubre")
dateFormatter.dateFromString("29 de octubre")
dateFormatter.dateFromString("30 de octubre")

我制作了此代码以在 Playground 中试用,但它重现了我在生产代码中获得的一些日期.看看这段代码产生的输出:

I made this code to try out in Playground, but it reproduces some dates I'm getting in my production code. Take a look in the output that this code produces:

正如您在图片中看到的,它不适用于 10 月 8 日和 15 日.适用于所有其他日期.我也试过八月和九月,在那几个月里整天都在工作.我还尝试在 pt-br 语言环境中用葡萄牙语输入日期,但也不适用于 10 月 8 日和 15 日.是否可以解释?

As you can see in the image, it doesn't work for October 8 and 15. Works for all the others. I also tried August and September, and in those months all days work. I also tried to put the dates in Portuguese in pt-br locale, and also doesn't work for October 8 and 15. Is it explainable?

更新:@zneak 代码为我生成以下输出:

Update: @zneak code produces the following output for me:

换句话说,10 月 8 日停止运行.

In other words, stopped running October 8.

推荐答案

你只需要设置你的日期格式化日历属性:

You just need to set your date formatter calendar property:

Xcode 8.2.1 • Swift 3.0.2

let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "es")
dateFormatter.dateFormat = "dd 'de' MMMM"
dateFormatter.date(from: "8 de octubre")   // "Oct 8, 2000, 1:00 AM"

这篇关于DateFormatter 在 Swift 中为没有时间的特定日期字符串返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆