2015-11-07 34 views
0

我正在制作一个应用程序,我从Parse获取时间为字符串,例如“13:24”我需要准备将它保存为CoreData作为日期,但用此字符串替换时间以便稍后在fireDate通知中使用它。什么是最好的方法来做到这一点?我应该得到currentDate(),将其更改为字符串,并以某种方式替换我的timeString从Parse在Swift中用包含时间的字符串替换当前日期

+0

你可能想看看NSDataDetector! – luk2302

回答

0

您可以使用此代码段获得今天的日期+自定义时间:

let time = "12:34" 

let dateFormatter = NSDateFormatter() 
dateFormatter.dateFormat = "yyyy/MM/dd HH:mm" 

var todayDateString = dateFormatter.stringFromDate(NSDate()) 
todayDateString.replaceRange(Range<String.Index>(start: todayDateString.endIndex.advancedBy(-5), end: todayDateString.endIndex), with: time) 

let newDate = dateFormatter.dateFromString(todayDateString) 
+0

谢谢!它会与'fireDate'兼容来触发通知? – kiuzio2

+0

'fireDate'通知是'NSDate?',与'newDate'相同,所以它必须兼容:) – sunshinejr

相关问题