我有一个应用程序,显示当天的时间表(海洋潮汐)。 我想要做的是能够利用时间并安排当时的当地通知。
如果我打开日期,当我点击单元格时,我得到“5/18/2016 03:31 AM”。 但是,如果我将其作为fireDate对象传递,则通知的fireDate为“nil”
如果我只是创建一个NSDate对象,在点击单元格后15秒触发,则fireDate为:“2016年5月18日星期三下午2:14:11”
所以我想真正的问题是,如何将“2016/5/18 03:31 AM”转换为“2016年5月18日星期三上午3:31:00”?
谢谢!
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". However, if I pass that in as the fireDate object, then the fireDate of the notification is "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"
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"?
Thank you!
最满意答案
您需要使用NSDateFormatter将日期格式化为所需的输出。 这是你如何做到这一点
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有关如何设置日期格式的更多信息,请参阅此链接
希望这可以帮助。 :)
You need to use a NSDateFormatter for formatting the date to the desired output. 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 cellFor more information on how to format dates, see this link
Hope this helps. :)
更多推荐
发布评论