NSDateFormatter的问题,在使用.dateFromString时返回nil

时间:2016-11-03 19:52:13

标签: swift swift2 nsdate xcode7 nsdateformatter

我遇到NSDateFormatter问题。我实施了一个日期选择器,您可以在其中选择两个日期来计算这两个日期之间的差异,并返回天数差异。这两个日期采用以下格式(.MediumStyle):

var firstDate = "Nov 3, 2016"
var lastDate = "Nov 9, 2016"

然而,当它到达名为calculateDifference()的函数中的部分时,这两个值被转换为NSDate类型,该方法返回nil

我的代码如下:

@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
    }
}

2 个答案:

答案 0 :(得分:0)

nil中获得calculateDifference的原因是您从未在日期格式化程序中设置日期格式或样式。

但根本没有理由使用字符串和日期格式。将所有内容保持为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
    }
}

答案 1 :(得分:0)

只需添加dateFormat

即可
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let firstDateAsNSDate = dateFormatter.dateFromString(firstDate) //returns nil
let lastDateAsNSDate = dateFormatter.dateFromString(lastDate) //returns nil
相关问题