Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Date type variables to initial value?

Tags:

date

swift

I can't find the way to set Date type variables to initial or empty value(not use a user defined initial value...like '0001/01/01').

Like String type can set String.init() or "" to it's value, so can use isEmpty() to determine it is empty, but how to do that with Date type variables?

like image 978
Edem Avatar asked Mar 02 '26 07:03

Edem


1 Answers

It depends on what you are going to accomplish.

Naturally there is no empty date.

  • If the value of the date is irrelevant declare it with the current date

    let date = Date()
    
  • If the date is supposed to be quite unspecified in the distant past or future there are

    let date = Date.distantPast
    
    let date = Date.distantFuture
    
  • If the date can be a specific date use the reference dates

    let date = Date(timeIntervalSince1970: 0) // 1970/1/1
    
    let date = Date(timeIntervalSinceReferenceDate: 0) // 2001/1/1
    
like image 133
vadian Avatar answered Mar 04 '26 03:03

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!