update
This commit is contained in:
@@ -8,6 +8,12 @@ import Foundation
|
||||
|
||||
public let DDDATE = DDDate.default
|
||||
|
||||
public enum DateFormatEnum : String {
|
||||
case yyyyMMddHHmmssDefault = "yyyy-MM-dd HH:mm:ss"
|
||||
case yyyyMMddDefault = "yyyy-MM-dd"
|
||||
case HHmmssDefault = "HH:mm:ss"
|
||||
}
|
||||
|
||||
open class DDDate {
|
||||
public static let `default` = DDDate()
|
||||
// 设置一个全局使用的
|
||||
@@ -55,8 +61,8 @@ extension Date {
|
||||
/// - locale: 本地化
|
||||
/// - timeZoneIdentifier: 时区
|
||||
/// - Returns: 时间的字符串格式
|
||||
public func dd_toString(format:String,locale:String? = nil,timeZoneIdentifier:String? = nil) -> String {
|
||||
return DDDATE.getDateFormatter(format: format,locale: locale,timeZoneIdentifier: timeZoneIdentifier).string(from: self)
|
||||
public func dd_toString(format:DateFormatEnum,locale:String? = nil,timeZoneIdentifier:String? = nil) -> String {
|
||||
return DDDATE.getDateFormatter(format: format.rawValue,locale: locale,timeZoneIdentifier: timeZoneIdentifier).string(from: self)
|
||||
}
|
||||
|
||||
/// 判断date是不是昨天
|
||||
@@ -103,14 +109,14 @@ extension Date {
|
||||
return "\(min)分钟前"
|
||||
}
|
||||
|
||||
return "今天\(dd_toString(format: "HH:mm:ss"))"
|
||||
return "今天\(dd_toString(format: .HHmmssDefault))"
|
||||
}
|
||||
|
||||
if isYesterday() == true {
|
||||
return "昨天\(dd_toString(format: "HH:mm:ss"))"
|
||||
return "昨天\(dd_toString(format: .HHmmssDefault))"
|
||||
}
|
||||
|
||||
return dd_toString(format: "yyyy-MM-dd HH:mm:ss")
|
||||
return dd_toString(format: .yyyyMMddHHmmssDefault)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,9 +126,9 @@ extension String {
|
||||
/// - dateString: 比较的时间
|
||||
/// - format: 比较时间的格式,时间比较需保持格式一致
|
||||
/// - Returns: 大于返回true
|
||||
public func dd_compare(dateString:String,format:String) -> ComparisonResult? {
|
||||
let date = dd_toDate(format: self)
|
||||
let compareDate = dd_toDate(format: self)
|
||||
public func dd_compare(dateString:String,format:DateFormatEnum) -> ComparisonResult? {
|
||||
let date = dd_toDate(format: format)
|
||||
let compareDate = dateString.dd_toDate(format: format)
|
||||
guard date != nil,compareDate != nil else {
|
||||
return nil
|
||||
}
|
||||
@@ -135,15 +141,15 @@ extension String {
|
||||
/// - locale: 本地化区域
|
||||
/// - timeZoneIdentifier: 时区
|
||||
/// - Returns: date
|
||||
public func dd_toDate(format:String,locale:String? = nil,timeZoneIdentifier:String? = nil) -> Date? {
|
||||
return DDDATE.getDateFormatter(format: format,locale: locale,timeZoneIdentifier: timeZoneIdentifier).date(from: self)
|
||||
public func dd_toDate(format:DateFormatEnum,locale:String? = nil,timeZoneIdentifier:String? = nil) -> Date? {
|
||||
return DDDATE.getDateFormatter(format: format.rawValue,locale: locale,timeZoneIdentifier: timeZoneIdentifier).date(from: self)
|
||||
}
|
||||
|
||||
|
||||
/// 将字符串转换成时间戳秒
|
||||
/// - Parameter format: 字符串时间格式
|
||||
/// - Returns: 时间戳
|
||||
public func dd_timeIntervalSince1970(format:String) -> TimeInterval? {
|
||||
public func dd_timeIntervalSince1970(format:DateFormatEnum) -> TimeInterval? {
|
||||
let date = dd_toDate(format: format)
|
||||
guard date != nil else {
|
||||
return nil
|
||||
@@ -154,7 +160,7 @@ extension String {
|
||||
/// 将字符串转换成时间戳毫秒
|
||||
/// - Parameter format: 字符串时间格式
|
||||
/// - Returns: 时间戳
|
||||
public func dd_timeIntervalSssSince1970(format:String) -> TimeInterval? {
|
||||
public func dd_timeIntervalSssSince1970(format:DateFormatEnum) -> TimeInterval? {
|
||||
let timeInterval = dd_timeIntervalSince1970(format: format)
|
||||
guard timeInterval != nil else {
|
||||
return nil
|
||||
@@ -166,7 +172,7 @@ extension String {
|
||||
/// 当前时间是不是昨天
|
||||
/// - Parameter format: 时间格式
|
||||
/// - Returns: 是不是
|
||||
public func isYesterday(format:String) -> Bool {
|
||||
public func isYesterday(format:DateFormatEnum) -> Bool {
|
||||
let date = dd_toDate(format: format)
|
||||
guard date != nil else {
|
||||
return false
|
||||
@@ -177,7 +183,7 @@ extension String {
|
||||
/// 当前时间是不是今天
|
||||
/// - Parameter format: 时间格式
|
||||
/// - Returns: 是不是
|
||||
public func isToday(format:String) -> Bool {
|
||||
public func isToday(format:DateFormatEnum) -> Bool {
|
||||
let date = dd_toDate(format: format)
|
||||
guard date != nil else {
|
||||
return false
|
||||
@@ -188,7 +194,7 @@ extension String {
|
||||
/// 当前时间是不是明天
|
||||
/// - Parameter format: 时间格式
|
||||
/// - Returns: 是不是
|
||||
public func isTomorrow(format:String) -> Bool {
|
||||
public func isTomorrow(format:DateFormatEnum) -> Bool {
|
||||
let date = dd_toDate(format: format)
|
||||
guard date != nil else {
|
||||
return false
|
||||
@@ -200,7 +206,7 @@ extension String {
|
||||
/// - Parameters:
|
||||
/// - format: 当前字符串时间的格式
|
||||
/// - Returns: 字符串
|
||||
public func dd_pushNotificationDateFormatString(format:String) -> String? {
|
||||
public func dd_pushNotificationDateFormatString(format:DateFormatEnum) -> String? {
|
||||
let date = dd_toDate(format: format)
|
||||
return date?.dd_pushNotificationDateFormatString()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user