KMAdvertisementTimeStampConversion.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // KMAdvertisementTimeStampConversion.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/29.
  6. //
  7. public class KMAdvertisementTimeStampConversion: NSObject {
  8. /**
  9. @abstract 时间string转时间戳
  10. @param dateString 时间戳字符串
  11. @return 时间
  12. */
  13. static func stringDateToTimestamp(dateString: String) -> TimeInterval{
  14. let formatter = DateFormatter()
  15. formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  16. let date = formatter.date(from: dateString)
  17. let time = date!.timeIntervalSince1970 * 1000
  18. return time
  19. }
  20. /**
  21. @abstract 时间戳转换成时间(HH:mm)
  22. @param timeString 时间戳字符串(毫秒)
  23. @return 时间string
  24. */
  25. static func timeStringToTimeSecond(timeString: String) -> String {
  26. let interval: TimeInterval = Double(timeString)! / 1000.0
  27. let date: Date = Date.init(timeIntervalSince1970: interval)
  28. let formatter = DateFormatter()
  29. formatter.dateFormat = "HH:mm"
  30. let dateString = formatter.string(from: date)
  31. return dateString
  32. }
  33. /**
  34. @abstract 时间string转date
  35. @param string 时间
  36. @return Date 时间
  37. */
  38. static func stringChangeToDate(string: String) -> Date {
  39. let formatter = DateFormatter()
  40. formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  41. formatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone?
  42. return formatter.date(from: string)!
  43. }
  44. /**
  45. @abstract date转时间string
  46. @param date 时间
  47. @return NSString 时间
  48. */
  49. static func dateChangeToString(date: Date) -> String {
  50. let formatter = DateFormatter()
  51. formatter.dateFormat = "yyyy-MM-dd"
  52. return formatter.string(from: date)
  53. }
  54. /**
  55. @abstract 获取当前的时间string
  56. @return NSString 时间
  57. */
  58. public static func getCurrentCompainedDateString() -> String {
  59. let date: Date = NSDate() as Date
  60. let formatter = DateFormatter()
  61. formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  62. let timeString = formatter.string(from: date)
  63. return timeString
  64. }
  65. /**
  66. @abstract 获取当前时间时间戳
  67. */
  68. public static func getCurrentTimeInterval () -> String {
  69. let date = NSDate.init()
  70. let timeSp: String = String(CLong(date.timeIntervalSince1970 * 1000))
  71. return timeSp
  72. }
  73. /**
  74. @abstract 判断是否是24小时之内
  75. @param timeStamp 时间
  76. @return bool
  77. */
  78. static func isInaDayWithTimestamp(timeStamp: TimeInterval) -> Bool {
  79. let currentTimeInterval:TimeInterval = Double(KMAdvertisementTimeStampConversion.getCurrentTimeInterval())!
  80. if (currentTimeInterval - timeStamp < 24 * 60 * 60) {
  81. return true
  82. } else {
  83. return false
  84. }
  85. }
  86. /**
  87. @abstract 获取当前的时间(年月日)
  88. @param timeStamp 时间
  89. @return bool
  90. */
  91. static func getCurrentTimeWithYMD() -> String{
  92. let date = NSDate()
  93. let formatter = DateFormatter()
  94. formatter.dateFormat = "yyyy-MM-dd"
  95. return formatter.string(from: date as Date)
  96. }
  97. }