小编典典

以字符串swift 3.0获取当前时间

swift

如何获取当前时间作为 字符串 (而不是日期)

日期时间格式。

yyyy-MM-dd HH:mm:ss


阅读 226

收藏
2020-07-07

共1个答案

小编典典

我就是这样想的。

   // Get today date as String

        func getTodayString() -> String{

                let date = Date()
                let calender = Calendar.current
                let components = calender.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)

                let year = components.year
                let month = components.month
                let day = components.day
                let hour = components.hour
                let minute = components.minute
                let second = components.second

                let today_string = String(year!) + "-" + String(month!) + "-" + String(day!) + " " + String(hour!)  + ":" + String(minute!) + ":" +  String(second!)

                return today_string

            }

        let today : String!

        today = getTodayString()
2020-07-07