ExtensionDate.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import Foundation
  2. extension Date{
  3. var isToday:Bool{
  4. let formatter = DateFormatter()
  5. formatter.dateFormat = "yyyyMMdd"
  6. let today = formatter.string(from: Date())
  7. let target = formatter.string(from: self)
  8. if today == target{
  9. return true
  10. }
  11. return false
  12. }
  13. }
  14. extension Date{
  15. func fromString(format:String) -> String {
  16. let formmater:DateFormatter = {
  17. let formatter = DateFormatter()
  18. formatter.locale = Locale.current
  19. formatter.dateFormat = format
  20. return formatter
  21. }()
  22. return formmater.string(from: self)
  23. }
  24. }
  25. extension Date{
  26. func fromWeekToSting() -> String?{
  27. let weekday = Calendar(identifier: .gregorian).component(.weekday, from: self)
  28. switch weekday {
  29. case 1:
  30. return "일요일"
  31. case 2:
  32. return "월요일"
  33. case 3:
  34. return "화요일"
  35. case 4:
  36. return "수요일"
  37. case 5:
  38. return "목요일"
  39. case 6:
  40. return "금요일"
  41. case 7:
  42. return "토요일"
  43. default:
  44. return nil
  45. }
  46. }
  47. func fromWeekToInt() -> Int {
  48. let weekday = Calendar(identifier: .gregorian).component(.weekday, from: self)
  49. return weekday
  50. }
  51. }
  52. extension Date{
  53. static func ==(lhs: Date, rhs: Date) -> Bool {
  54. return lhs.fromString(format: "yyyyMMdd") == rhs.fromString(format: "yyyyMMdd")
  55. }
  56. }
  57. extension Date{
  58. func adding(forYear:Int) -> Date? {
  59. return Calendar.current.date(byAdding: .year, value: forYear, to: self)
  60. }
  61. }