最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift获取当前日期和时间

Swift crifan 4193浏览 0评论
swift get current time
最后用代码:
/***************************************************************************
 * Date Time Related functions
 ***************************************************************************/

extension NSDate
{
    //2015
    func Year() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentYear:Int = curCalendar.component(NSCalendarUnit.Year, fromDate: self)
        return componentYear
    }

    //11
    func Month() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentMonth:Int = curCalendar.component(NSCalendarUnit.Month, fromDate: self)
        return componentMonth
    }

    //28
    func Day() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentDay:Int = curCalendar.component(NSCalendarUnit.Day, fromDate: self)
        return componentDay
    }

    //10
    func Hour() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentHour:Int = curCalendar.component(NSCalendarUnit.Hour, fromDate: self)
        return componentHour
    }

    //39
    func Minute() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentMinute:Int = curCalendar.component(NSCalendarUnit.Minute, fromDate: self)
        return componentMinute
    }

    //18
    func Second() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentSecond:Int = curCalendar.component(NSCalendarUnit.Second, fromDate: self)
        return componentSecond
    }

    //get short style date time string
    //11/28/15, 10:51 AM
    func toStringShort() -> String {
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
        dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
        let shortStyleStr:String = dateFormatter.stringFromDate(self)
        return shortStyleStr
    }

    //get medium style date time string
    //Nov 28, 2015, 10:51:33 AM
    func toStringMedium() -> String {
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
        dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle
        let mediumStyleStr:String = dateFormatter.stringFromDate(self)
        return mediumStyleStr
    }

    //get long style date time string
    //November 28, 2015 at 10:51:33 AM GMT+8
    func toStringLong() -> String {
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
        dateFormatter.timeStyle = NSDateFormatterStyle.LongStyle
        let longStyleStr:String = dateFormatter.stringFromDate(self)
        return longStyleStr
    }

    //get full style date time string
    //Saturday, November 28, 2015 at 10:51:33 AM China Standard Time
    func toStringFull() -> String {
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
        dateFormatter.timeStyle = NSDateFormatterStyle.FullStyle
        let fullStyleStr:String = dateFormatter.stringFromDate(self)
        return fullStyleStr
    }

    //get date formatted string
    //2015/11/28 10:48:12
    func toString(dateFormat:String) -> String {
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = dateFormat
        let formattedDatetimeStr:String = dateFormatter.stringFromDate(self)
        return formattedDatetimeStr
    }

    //parse input date time string into NSDate
    //input: 2015/11/28 12:01:02 and yyyy/MM/dd HH:mm:ss
    //output: Optional(2015-11-28 04:01:02 +0000)
    static func fromString(datetimeStr:String, dateFormat:String) -> NSDate? {
        let dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = dateFormat
        let parsedDatetime:NSDate? = dateFormatter.dateFromString(datetimeStr)
        return parsedDatetime
    }
}
测试代码:
   /*
    * NSdate extension demo
    */
    let curDate:NSDate = NSDate()
    print("curDate=\(curDate)") //2015-11-28 02:35:19 +0000
    print("curDate.Year()=\(curDate.Year())") //2015
    print("curDate.Month()=\(curDate.Month())") //11
    print("curDate.Day()=\(curDate.Day())") //28
    print("curDate.Hour()=\(curDate.Hour())") //10
    print("curDate.Minute()=\(curDate.Minute())") //39
    print("curDate.Second()=\(curDate.Second())") //18

    print("curDate.toShortStyleString()=\(curDate.toStringShort())") //11/28/15, 11:04 AM
    print("curDate.toMediumStyleString()=\(curDate.toStringMedium())") //Nov 28, 2015, 11:04:39 AM
    print("curDate.toLongStyleString()=\(curDate.toStringLong())") //November 28, 2015 at 11:04:39 AM GMT+8
    print("curDate.toFullStyleString()=\(curDate.toStringFull())") //Saturday, November 28, 2015 at 11:04:39 AM China Standard Time

    let formatedDateStr:String = curDate.toString("yyyy/MM/dd HH:mm:ss") //2015/11/28 11:00:33
    print("formatedDateStr=\(formatedDateStr)")

    let parsedDatetime:NSDate? = NSDate.fromString("2015/11/28 12:01:02", dateFormat: "yyyy/MM/dd HH:mm:ss")
    print("parsedDatetime=\(parsedDatetime)") //Optional(2015-11-28 04:01:02 +0000)
注:
更多函数详见:
https://github.com/crifan

转载请注明:在路上 » [已解决]Swift获取当前日期和时间

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.248 seconds, using 22.12MB memory