我正在制作一个应用程序,我可以从 Parse as String 中获取时间,例如“13:24”我需要准备将其保存在 CoreData 中作为日期,但用此字符串替换时间以便稍后在 fireDate 通知中使用它。做到这一点的最佳方法是什么?我应该获取 currentDate()
,将其更改为字符串,并以某种方式将其替换为 Parse
中的 timeString ?
最佳答案
您可以使用此代码段来获取今天的日期 + 自定义时间:
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)
关于ios - 在 Swift 中用包含时间的字符串替换当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33585336/