简体   繁体   English

如何快速将日期,月份和年份转换为毫秒

[英]How to get Date, Month and Year into Millisecond in swift

Hi am Trying to convert year, month date into Millisecond how to do please help me to do this, 嗨,我正在尝试将年,月日期转换为毫秒,该怎么做,请帮助我完成此操作,

Bellow Is my Code that i have try. 波纹管是我尝试过的代码。

let date:String = "2018-08-04T07:34:15.287Z"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy-MM-dd"

var dateFromString = Date()
if let aString = dateFormatter.date(from: date) {
    dateFromString = aString
}

let timeInMiliseconds:Int = Int(TimeInterval(dateFromString.timeIntervalSince1970 * 1000))

return timeInMiliseconds

But I Alway Got DateTime Into Millisecond. 但是我总是将DateTime设置为毫秒。 when I am trying to get the only date into millisecond please help I need help. 当我尝试将唯一的日期设为毫秒时,请帮助我需要帮助。 Thanks in advance. 提前致谢。

Your dateFormat is not match with date. 您的dateFormat与日期不匹配。 Try below code: 试试下面的代码:

let date:String = "2018-08-04T07:34:15.287Z"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let dateToConvert = dateFormatter.date(from: date)

dateFormatter.dateFormat = "yyyy-MM-dd"
let finalDate = dateFormatter.string(from: dateToConvert!)

var dateFromString = Date()
if let aString = dateFormatter.date(from: finalDate) {
    dateFromString = aString
}

let timeInMiliseconds:Int = Int(TimeInterval(dateFromString.timeIntervalSince1970 * 1000))

You will get 1533321000000 in timeInMiliseconds 您将在15秒内获得1533321000000

You can check time here 您可以在这里查看时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM