swift NSDateFormatter
//
// TimeViewController.swift
// UIControlDemo
//
// Created by on 14/12/10.
// Copyright (c) 2014年 马大哈. All rights reserved.
//
import UIKit
class TimeViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Time/Date"
// 时间格式
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
// 按照上面格式输出 现在的时间
var date = NSDate(timeIntervalSinceNow: NSTimeIntervalSince1970)
var dateString = dateFormatter.stringFromDate(date)
// 按上面格式输出 NSDate类型(转换字符串)
var dateDate = dateFormatter.dateFromString("2014-12-19 17:12:12")//返回NSDate
println(dateDate?.description)
// yes 说明date早,反之"2014-12-19 17:12:12"早
timeCompare(dateEarlier: date, dateLater: "2014-12-19 17:12:12")
}
func timeCompare(#dateEarlier:NSDate ,dateLater:String ) ->Bool{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
var dateLater = dateFormatter.dateFromString("2013-12-19 12:12:12")//返回NSDate
var compare = dateEarlier.earlierDate(dateLater!)
if dateEarlier.isEqualToDate(compare) {
return true
}else{
return false
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}