| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- //
- // DateUtils.swift
- // MTP2_iOS
- //
- // Created by zhongyuan on 2018/6/12.
- // Copyright © 2018年 zhongyuan.All rights reserved.
- //
- import Foundation
- /// 日期工具类
- class DateUtils {
- /// 获取指定时间上一个月的时间
- ///
- /// - Parameter date: 不填时获取当前时间
- /// - Returns: 上一个月的时间
- static func getLastDate(_ date: Date = Date()) -> Date {
- var comps = DateComponents()
- comps.month = -1
- let calender = Calendar(identifier: .gregorian)
- let mdate = calender.date(byAdding: comps, to: date)
- return mdate!
- }
-
- /// 获取任一月份的时间
- /// - Parameters:
- /// - month: month
- /// - date: date
- static func getDiffMonthDate(month: Int, _ date: Date = Date()) -> Date {
- var comps = DateComponents()
- comps.month = month
- let calender = Calendar(identifier: .gregorian)
- let mdate = calender.date(byAdding: comps, to: date)
- return mdate!
- }
-
- /// 获取任一天数的时间
- /// - Parameters:
- /// - month: month
- /// - date: date
- static func getDiffDayhDate(day: Int, _ date: Date = Date()) -> Date {
- var comps = DateComponents()
- comps.day = day
- let calender = Calendar(identifier: .gregorian)
- let mdate = calender.date(byAdding: comps, to: date)
- return mdate!
- }
-
- static func getNextMonthDate(_ date: Date = Date()) -> Date {
- var comps = DateComponents()
- comps.month = 1
- let calender = Calendar(identifier: .gregorian)
- let mdate = calender.date(byAdding: comps, to: date)
- return mdate!
- }
-
- static func getSevenDaysAgo(_ date: Date = Date()) -> Date {
- var comps = DateComponents()
- comps.day = -7
- let calender = Calendar(identifier: .gregorian)
- let mdate = calender.date(byAdding: comps, to: date)
- return mdate!
- }
-
- /// 获取本年开始日期的方法
- static func startOfCurrentYear() -> Date {
- let date = Date()
- let calendar = Calendar.current
- let components = calendar.dateComponents(Set<Calendar.Component>([.year]), from: date)
- let startOfYear = calendar.date(from: components)!
- return startOfYear
- }
-
- /// 获取本月开始日期的方法
- static func startOfCurrentMonth() -> Date {
- let date = Date()
- let calendar = Calendar.current
- let components = calendar.dateComponents(Set<Calendar.Component>([.year, .month]), from: date)
- let startOfYear = calendar.date(from: components)!
- return startOfYear
- }
-
- /// 判断两个日期是否在同年的同一个月
- static func judgeSameYearMonth(target: Date, source: Date = Date()) -> Bool {
- let calendar = Calendar.current
- let targetComponents = calendar.dateComponents(Set<Calendar.Component>([.year, .month]), from: target)
- let sourceComponents = calendar.dateComponents(Set<Calendar.Component>([.year, .month]), from: source)
- if calendar.date(from: targetComponents)!.compare(calendar.date(from: sourceComponents)!) == .orderedSame {
- return true
- }
- return false
- }
-
- /// 根据时间戳获取对应的时间信息
- /// - Parameters:
- /// - timeInterval: timeInterval
- /// - formatter: formatter
- /// - Returns: String
- static func dateStringSince1970(_ timeInterval: TimeInterval, _ formatter: String = "yyyy-MM-dd") -> String {
- return Date(timeIntervalSince1970: timeInterval/1000).getString(formatter: formatter)
- }
-
- /// 获取带T的时间戳的时间字符串
- /// - Parameter dateString: dateString
- /// - Returns: String
- static func getTDateString(_ dateString: String, _ formatter: String = "yyyy-MM-dd") -> String {
- let dateFormatter = DateFormatter()
- let tempLocale = dateFormatter.locale
- dateFormatter.locale = Locale(identifier: "en_US_POSIX")
- dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
- let date = dateFormatter.date(from: dateString) ?? Date()
- dateFormatter.dateFormat = formatter
- dateFormatter.locale = tempLocale
- return dateFormatter.string(from: date)
- }
-
- /// 获取两个时间戳间隔时分秒
- /// - Parameters:
- /// - timer1: timer1
- /// - timer2: timer2
- /// - count: count
- /// - Returns: String
- static func diffs(_ timer1: TimeInterval, _ timer2: TimeInterval, _ count: Double) -> DateComponents? {
-
- let formatter = DateFormatter()
- formatter.dateStyle = .medium
- formatter.timeStyle = .short
- formatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
-
- let date1 = Date(timeIntervalSince1970: timer1-count)
- let date2 = Date(timeIntervalSince1970: timer2)
-
- /// 日历对象(方便比较两个日期之间的差距)
- let calendar = Calendar.current
- return calendar.dateComponents(Set<Calendar.Component>([.hour, .minute, .second]), from: date1, to: date2)
- }
- }
|