• Gets the current timestamp
  • Gets the current time string
  • Timestamp converts the time string
  • Date to the Date
  • Date to timestamp
  • Timestamp conversion time date
  • Gets (year, month, day, hour, minute, second) date-to-time stamps
  • Get time string

code

import Foundation


extension Date {
    
    /// get the current timestamp
    /// - Returns: indicates the current timestamp
    static func getNowTimeStamp(a) -> Int {
        let nowDate = Date.init(a)//10 digit timestamp
        let interval = Int(nowDate.timeIntervalSince1970)
        return interval
    }
    
    /// Gets the current time string
    /// - Returns: indicates the current timestamp
    static func getNowTimeString(dateFormat: String) -> String {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = dateFormat
        let nowDate = Date.init(a)return dateformatter.string(from: nowDate)
    }
    
    
    
    /// Timestamp converts the time string
    /// - Parameters:
    /// -timestamp: timeStamp
    /// -dateformat: user-defined dateFormat (such as yyyy-mm-dd HH: MM :ss)
    /// - Returns: Time string
    static func getTimeString(timeStamp: Int.dateFormat: String) -> String {
        let date = Date(timeIntervalSince1970: TimeInterval.init(timeStamp))
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = dateFormat
        return dateformatter.string(from: date)
    }
    
    
    /// Change the Date to Date
    /// - Parameters:
    /// -timeString: indicates the date string
    /// -dateformat: user-defined dateFormat (such as yyyy-mm-dd HH: MM :ss)
    /// - Returns: Date
    static func getDate(timeString: String.dateFormat: String) -> Date {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = dateFormat
        let date = dateformatter.date(from: timeString) ?? Date(a)return date
    }
    
    /// date to time stamp
    /// - Parameters:
    /// -timeString: indicates the date string
    /// -dateformat: user-defined dateFormat (such as yyyy-mm-dd HH: MM :ss)
    /// - Returns: timestamp
    static func getTimeStamp(timeString: String.dateFormat: String) -> Int {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = dateFormat
        let date = self.getDate(timeString: timeString, dateFormat: dateFormat)
        return Int(date.timeIntervalSince1970)
    }
    
    
    /// the timestamp is converted to date
    /// - Parameters:
    /// -timestamp: timeStamp
    /// - Returns: date
    static func getDateWith(timeStamp: Int) -> Date {
        let date = Date(timeIntervalSince1970: TimeInterval.init(timeStamp))
        return date
    }
    
    
    /// get (year, month, day, hour, minute, second)
    /// - Returns: (year, month, day, hour, minute, second)
    func getTime(a)- > (String.String.String.String.String.String) {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = "yyyy"
        let y = dateformatter.string(from: self)
        dateformatter.dateFormat = "MM"
        let mo = dateformatter.string(from: self)
        dateformatter.dateFormat = "dd"
        let d = dateformatter.string(from: self)
        dateformatter.dateFormat = "HH"
        let h = dateformatter.string(from: self)
        dateformatter.dateFormat = "mm"
        let m = dateformatter.string(from: self)
        dateformatter.dateFormat = "ss"
        let s = dateformatter.string(from: self)
        
        return (y, mo, d, h, m, s)
            }
    
    
    /// Get the time string
    /// -parameter dateFormat: user-defined dateFormat (for example, yyyy-mm-dd HH: MM :ss)
    /// - Returns: Time string
    func getStringTime(dateFormat: String) -> String {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = dateFormat
        return dateformatter.string(from: self)}}Copy the code

Demo: Github