[英]Problems with NSDateFormatter, returns nil when using .dateFromString
I'm having problems with NSDateFormatter. 我在使用NSDateFormatter时遇到问题。 I implemented a date picker where you can choose two dates to calculate the difference between these two dates and return the difference in days.
我实现了一个日期选择器,您可以在其中选择两个日期来计算这两个日期之间的差并返回以天为单位的差。 The two dates are in the following format (.MediumStyle):
这两个日期采用以下格式(.MediumStyle):
var firstDate = "Nov 3, 2016"
var lastDate = "Nov 9, 2016"
However when it gets to the part in the function called calculateDifference()
where these two values are being converted to an type of NSDate
, the method returns nil
. 但是,当到达函数
calculateDifference()
中将这两个值转换为NSDate
类型的部分时,该方法将返回nil
。
My code is as follows: 我的代码如下:
@IBOutlet weak var startDateOutput: UILabel! // shows start date user picked
@IBOutlet weak var endDateOutput: UILabel! // shows end date user picked
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var answerFieldTimeDifference: UILabel! // Output Field
var firstDate = ""
var lastDate = ""
@IBAction func startButton(sender: AnyObject) // Button to set firstDate
{
firstDate = NSDateFormatter.localizedStringFromDate(datePicker.date, dateStyle: NSDateFormatterStyle.MediumStyle, timeStyle: NSDateFormatterStyle.NoStyle)
startDateOutput.text = firstDate
calculateDifference()
}
@IBAction func expirationButton(sender: AnyObject) // Button to set lastDate
{
lastDate = NSDateFormatter.localizedStringFromDate(datePicker.date, dateStyle: NSDateFormatterStyle.MediumStyle, timeStyle: NSDateFormatterStyle.NoStyle)
endDateOutput.text = lastDate
calculateDifference()
}
func calculateDifference()
{
if !firstDate.isEmpty && !lastDate.isEmpty
{
let dateFormatter = NSDateFormatter()
let firstDateAsNSDate = dateFormatter.dateFromString(firstDate) //returns nil
let lastDateAsNSDate = dateFormatter.dateFromString(lastDate) //returns nil
let dateComponentsFormatter = NSDateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [NSCalendarUnit.Day]
var calculatedDifference = dateComponentsFormatter.stringFromDate(firstDateAsNSDate!, toDate: lastDateAsNSDate!)
answerFieldTimeDifference.text = calculatedDifference
}
}
The reason you get nil
in calculateDifference
is because you never set a date format or style to the date formatter. 在
calculateDifference
得到nil
的原因是因为您从未为日期格式化程序设置日期格式或样式。
But there is no reason at all to use strings and date formats. 但是根本没有理由使用字符串和日期格式。 Keep everything as
NSDate
and avoid all of the needless extra work. 将所有内容保留为
NSDate
并避免所有不必要的额外工作。
@IBOutlet weak var startDateOutput: UILabel! // shows start date user picked
@IBOutlet weak var endDateOutput: UILabel! // shows end date user picked
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var answerFieldTimeDifference: UILabel! // Output Field
var firstDate : NSDate?
var lastDate : NSDate?
@IBAction func startButton(sender: AnyObject) // Button to set firstDate
{
firstDate = datePicker.date
let dateStr = NSDateFormatter.localizedStringFromDate(firstDate, dateStyle: NSDateFormatterStyle.MediumStyle, timeStyle: NSDateFormatterStyle.NoStyle)
startDateOutput.text = dateStr
calculateDifference()
}
@IBAction func expirationButton(sender: AnyObject) // Button to set lastDate
{
lastDate = datePicker.date
let dateStr = NSDateFormatter.localizedStringFromDate(lastDate, dateStyle: NSDateFormatterStyle.MediumStyle, timeStyle: NSDateFormatterStyle.NoStyle)
endDateOutput.text = dateStr
calculateDifference()
}
func calculateDifference()
{
if let firstDate = firstDate, let lastDate = lastDate
{
let dateComponentsFormatter = NSDateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [NSCalendarUnit.Day]
var calculatedDifference = dateComponentsFormatter.stringFromDate(firstDate, toDate: lastDate)
answerFieldTimeDifference.text = calculatedDifference
}
}
Just add a dateFormat 只需添加一个dateFormat
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let firstDateAsNSDate = dateFormatter.dateFromString(firstDate) //returns nil
let lastDateAsNSDate = dateFormatter.dateFromString(lastDate) //returns nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.