繁体   English   中英

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

[英]Problems with NSDateFormatter, returns nil when using .dateFromString

我在使用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
    }
}

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

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

只需添加一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM