Swift - Get local date and time

前端 未结 10 689
挽巷
挽巷 2020-12-17 08:51

How can I get local date and time in Swift?

let last_login = String(NSDate.date())
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 09:17

    Swift 4

    To get current date and time

    let currentDate = Date()
    print(currentDate) //this will return current date and time 
    

    but that will be in date type to convert date into string

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy HH:mm" //give the formate according to your need
    let dateStr =  dateFormatter.string(from: currentDate) //which will give the string of current date and time in required dateformate
    

提交回复
热议问题