簡體   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