2016-11-03 2 views
0

NSDateFormatterに問題があります。 2つの日付を選択してこれらの2つの日付の差を計算し、差を返す日付ピッカーを実装しました。しかし、それはこれらの2つの値は、メソッドがnilを返し、NSDateの型に変換されてcalculateDifference()呼び出された関数内の部分になったときNSDateFormatterの問題.dateFromString使用時にnilを返します。

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

:二つの日付は、次の形式(.MediumStyle)です。次のように

私のコードは次のとおりです。

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

を追加します。 2つの 'NSDate'オブジェクトへの参照を保持してください。 – rmaddy

答えて

0

日付フォーマッタに日付形式やスタイルを設定することはありませんので、あなたがcalculateDifferencenilを取得する理由はあります。

しかし、文字列と日付形式を使用する理由はまったくありません。すべてを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 
    } 
} 
+0

ありがとうございました!感謝します。すべてが今働きます。良い一日を! – heisenberg

0

ちょうどそれがすべてで文字列を使用しても意味がありませんDATEFORMAT

let dateFormatter = NSDateFormatter() 
dateFormatter.dateFormat = "yyyy-MM-dd" 
let firstDateAsNSDate = dateFormatter.dateFromString(firstDate) //returns nil 
let lastDateAsNSDate = dateFormatter.dateFromString(lastDate) //returns nil 
関連する問題