美文网首页程序员
关于Date的一些常用的方法总结

关于Date的一些常用的方法总结

作者: BeSt2wazi | 来源:发表于2017-03-10 14:44 被阅读0次

最近在新的项目中用了很多关于时间计算的东西,各种需求也是让人难受的不行,其中也遇到不少关于时区的坑,特此前来总结一下关于date的常用延展方法,以后还会陆续补充

把当前时间转换成当前时区

func passDate() -> Date{
    let zone = NSTimeZone.system
    let time:Int = zone.secondsFromGMT(for: self)
    let nowDate = self.addingTimeInterval(TimeInterval(time))
    return nowDate
}

date转字符串

/// - Parameters:
///   - date: 某天
///   - string: 时间格式
/// - Returns: string
func getSomeDayToString(date:Date,string:String) -> String {
    let nowDate = date
    let timeZone = TimeZone.init(identifier: "UTC")
    let formatter = DateFormatter()
    formatter.timeZone = timeZone
    formatter.locale = Locale.init(identifier: "zh_CN")
    formatter.dateFormat = string
    return formatter.string(from: nowDate)
}

获取某天的指定时间

/// - Parameter time: 时间 (19:00)
/// - Returns: date
func getCurrentDaySomeTime(time:String,someday:Date) -> Date {
    let timeZone = TimeZone.init(identifier: "UTC")
    let formatter = DateFormatter()
    formatter.timeZone = timeZone
    formatter.locale = Locale.init(identifier: "zh_CN")
    formatter.dateFormat = "yyyy年MM月dd日"
    var today = formatter.string(from: someday)
    today = today + " " + time
    let newformatter = DateFormatter()
    newformatter.dateFormat = "yyyy年MM月dd日 HH:mm"
    newformatter.timeZone = timeZone
    newformatter.locale = Locale.init(identifier: "zh_CN")
    let newDate = newformatter.date(from: today)!
    return newDate
}

date转时间戳

func dateToTimestamp(date:Date) -> TimeInterval {
    let invale = date.timeIntervalSince1970
    return invale
}

获取周六晚上七点的时间

func getSatdayNight()->Date{
    
    let gmt = NSTimeZone(abbreviation: "GMT")
    let gregorian = NSCalendar(identifier: .gregorian)
    gregorian?.timeZone = gmt as! TimeZone
    var components = gregorian?.components([ .weekday,.day,.hour,.minute,.second], from: self)
    components?.setValue(6 - (components?.weekday)!, for: .weekday)
    components?.setValue(1, for: .day)
    components?.setValue(19 - (components?.hour)! - 1, for: .hour)
    components?.setValue(60 - (components?.minute)! - 1, for: .minute)
    components?.setValue(60 - (components?.second)!, for: .second)
    return (gregorian?.date(byAdding: components!, to: self, options: NSCalendar.Options(rawValue: 0))!)!
    
}

昨天

func getLastDay() ->Date {

    let calendar  = NSCalendar.init(identifier: .gregorian)
    var comps = calendar?.components(.day, from: self)
    comps?.setValue(-1, for: .day)
    
    return (calendar?.date(byAdding: comps!, to: self, options: NSCalendar.Options(rawValue: 0))!)!
    
}

前三天

func getLastThreeDay() ->Date {
    
    let calendar  = NSCalendar.init(identifier: .gregorian)
    var comps = calendar?.components(.day, from: self)
    comps?.setValue(-2, for: .day)
    
    return (calendar?.date(byAdding: comps!, to: self, options: NSCalendar.Options(rawValue: 0))!)!
    
}

今天稍后两小时

func getTodaylater() -> Date{
    let calendar  = NSCalendar.init(identifier: .gregorian)
    var comps = calendar?.components(.hour, from: self)
    comps?.setValue(2, for: .hour)

    return (calendar?.date(byAdding: comps!, to: self, options: NSCalendar.Options(rawValue: 0))!)!
}

比较当前时间和指定时间

/// - Parameter customDate: 要比较的时间
/// - Returns: 早于当前时间返回YES,否则NO
func compareTime(customDate:Date) -> Bool {
    let isTrue:ComparisonResult = self.passDate().compare(customDate)
    if isTrue == ComparisonResult.orderedAscending {
        return true
    }else{
        return false
    }
}

相关文章

  • 关于Date的一些常用的方法总结

    最近在新的项目中用了很多关于时间计算的东西,各种需求也是让人难受的不行,其中也遇到不少关于时区的坑,特此前来总结一...

  • 2017/3/27

    总结一下关于date的一些静态方法: 原生: moment.js

  • Date常用方法总结

    被日历折磨了3天,发现关于Date的基本转换在此时显得格外重要!现在来记录一下关于这个部分的相关知识,用烂笔头来弥...

  • Date、DateFormat和Calendar

    目录​Date​-------Date类的构造函数​-------Date类中的常用方法​DateFormatSi...

  • DateUtils学习笔记

    一、DateUtils常用方法 1.1.常用的日期判断 isSameDay(final Date date1, f...

  • JavaScript_对象

    一、时间对象(Date) (一)Date对象的创建 (二)Date对象的方法 (三)设置与获取的常用方法 二、对象...

  • UIApplication sharedApplication总

    UIApplication sharedApplication 常用的一些方法总结!

  • Date 常用方法

    Date 常用方法 Date对象实例 不加参数的话,实例代表的是当前时间 前面加 “+”号可以将其转化成时间毫秒数...

  • UIPickerView和UICollectionView

    PickerView常用方法总结 DataSource -Delegate UICollectionVIew的一些...

  • Java SE 2

    Java SE 1.Date 和 DateFormat Date类及常用方法 java.util.Date 类用于...

网友评论

    本文标题:关于Date的一些常用的方法总结

    本文链接:https://www.haomeiwen.com/subject/yoqwgttx.html