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

最后用代码:
/***************************************************************************
 * 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)
注:
更多函数详见:


发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量