【问题标题】:Problems with NSDateFormatter, returns nil when using .dateFromStringNSDateFormatter 的问题,使用 .dateFromString 时返回 nil
【发布时间】:2017-03-17 13:05:48
【问题描述】:

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

【问题讨论】:

  • 使用字符串完全没有意义。只需保留对两个 NSDate 对象的引用即可。

标签: swift swift2 nsdate xcode7 nsdateformatter


【解决方案1】:

您在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
    }
}

【讨论】:

  • 谢谢先生!欣赏它。现在一切正常。祝你有美好的一天!
【解决方案2】:

只需添加一个日期格式

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let firstDateAsNSDate = dateFormatter.dateFromString(firstDate) //returns nil
let lastDateAsNSDate = dateFormatter.dateFromString(lastDate) //returns nil

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    相关资源
    最近更新 更多