-3

How to display a date "jan-01-0000"? Year 0 can not be managed. How to do ?

Thanks for your help.

let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM.dd.yyyy"
let Date_Str = "01.01.0000"
print (dateFormatter.date(from: Date_Str)!)

--> The program shows me an error: Year 0 can not be managed.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Dominique
  • 1
  • 5
  • 4
    That is not a valid `Date`, why would you want to use that? The minimum `Date` value is "0001-01-01" as discussed in [this Q&A](https://stackoverflow.com/questions/15156996/nsdate-min-and-max-possible-values). – Dávid Pásztor Dec 10 '17 at 18:29
  • it is to make it a time machine. I think I have a solution. I'll have to trick on the date with String year = year - 1 – Dominique Dec 10 '17 at 20:12
  • 1
    [Year 0 simply does not exist.](https://en.wikipedia.org/wiki/Year_zero) There's 1 BC and then there's 1 AD. – deceze Dec 10 '17 at 20:51

1 Answers1

-2
class ViewController: UIViewController {

    let dateFormatter: DateFormatter = DateFormatter()
    var split_date: [String]!
    let Date_Str = "01.01.0001.00.00"
    var _isBeforeFirstYear: Bool = true

    override func viewDidLoad() {
        super.viewDidLoad()

        dateFormatter.dateFormat = "MM.dd.yyyy.HH.mm"
        split_date = Date_Str.components(separatedBy: ".")

        if (_isBeforeFirstYear) {
            split_date[2] = String(format: "%04d", (Int(split_date[2]))!-1)
            print (split_date[2])  // year = 0000
        } else {
            print (split_date[2])  // year = 0001
        }
    }
}
Dominique
  • 1
  • 5