我找不到将Date类型变量设置为初始值或空值的方法(而不是使用用户定义的初始value...like '0001/01/01')。
像String类型可以将String.init()或"“设置为它的值,所以可以使用isEmpty()来确定它是空的,但是如何使用Date类型变量呢?
发布于 2019-03-18 15:46:33
这取决于你要完成什么。
当然,没有空的日期。
让
date如果在遥远的过去或将来没有指定日期,则有
let date = Date.distantFuture
let date = Date(timeIntervalSince1970: 0) // 1970/1/1 let date = Date(timeIntervalSinceReferenceDate: 0) // 2001/1/1
发布于 2019-03-18 14:30:07
您可以通过Date对象的默认构造函数初始化该对象。
var date = Date() 上面这行代码将用当前日期初始化date对象。
希望这能有所帮助
发布于 2019-03-18 14:34:56
您可以声明一个Date = nil,这意味着当我们为数据赋值时,比如
var date = Date() // means its current data或else
使用以下代码从字符串格式设置日期
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26") {
print(dateFormatterPrint.string(from: date))
} else {
print("There was an error decoding the string")
}我希望这对你有帮助
https://stackoverflow.com/questions/55215654
复制相似问题