简体   繁体   English

将格式化日期转换为NSDate

[英]Convert formatted date to NSDate

I have an app that displays a tableview of times in the day (oceanic tides). 我有一个应用程序,可以显示一天中的时间(海洋潮汐)的表格视图。 What I want to do is be able to tap on a time and schedule a local notification to fire at that time. 我想做的是能够轻按一下时间并计划在该时间触发本地通知。

If I print out the date when I tap the cell I get "5/18/2016 03:31 AM". 如果我在点击该单元格时打印出日期,则会得到“ 5/18/2016 03:31 AM”。 However, if I pass that in as the fireDate object, then the fireDate of the notification is "nil" 但是,如果我将其作为fireDate对象传递,则通知的fireDate为“ nil”

If I simply create an NSDate object to fire 15 seconds after tapping the cell, then the fireDate is: "Wednesday, May 18, 2016 at 2:14:11 PM" 如果我仅创建一个NSDate对象以在点击单元格后15秒钟内触发,则fireDate为:“ 2016年5月18日,星期三,下午2:14:11”

So I guess the real question is, how can I convert "5/18/2016 03:31 AM" to "Wednesday, May 18, 2016 at 3:31:00 AM"? 所以我想真正的问题是,如何将“ 5/18/2016 03:31 AM”转换为“ 2016年5月18日,星期三,3:31:00 AM”?

Thank you! 谢谢!

You need to use a NSDateFormatter for formatting the date to the desired output. 您需要使用NSDateFormatter将日期格式化为所需的输出。 Here is how you can do that 这是你可以做到的

let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.FullStyle
let dateToFire = formatter.dateFromString(datefromcell) //datefromcell is a string that contains the date you got from tapping the tableview cell

For more information on how to format dates, see this link 有关如何设置日期格式的更多信息,请参见此链接。

Hope this helps. 希望这可以帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM