0

I am trying to convert passed NSDate into a readable Date/Time String for a UILabel. For some reason the label is showing unexpected dates in the output.

struct myDateRange{
    var startDate: NSNumber?
    var endDate: NSNumber?
}

func configureDate(date:myDateRange){

    let date = NSDate(timeIntervalSince1970: date.startDate as! TimeInterval)
    let timeFormat = DateFormatter()
    timeFormat.timeStyle = .short
    timeFormat.dateStyle = .short
    let currentTime = "\(timeFormat.string(from: date as Date))"
    timeLabel.text = String(currentTime)
}

Output:

passed: Optional(1518393600000)

labeltext: 12/23/85, 10:30 AM

Expected:

02/12/18, 12:00

Ben
  • 295
  • 5
  • 19
  • 1
    Your number is in milliseconds, not seconds. Simply divide by 1000. And you should not declare your own `Date` struct. Swift provides one already. Your name is going to cause way too much confusion in your code. – rmaddy Nov 09 '19 at 00:23
  • Thanks @rmaddy updated and fixed with your comment – Ben Nov 09 '19 at 00:45

0 Answers0