将格式化日期转换为NSDate(Convert formatted date to NSDate)

编程入门 行业动态 更新时间:2024-10-28 11:29:08
将格式化日期转换为NSDate(Convert formatted date to NSDate)

我有一个应用程序,显示当天的时间表(海洋潮汐)。 我想要做的是能够利用时间并安排当时的当地通知。

如果我打开日期,当我点击单元格时,我得到“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 cell

For more information on how to format dates, see this link

Hope this helps. :)

更多推荐

本文发布于:2023-08-05 03:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1426145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   日期   NSDate   date   formatted

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!