Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import UIKit
  2.  
  3. class DateAndTime {
  4. let hourFormatter = DateFormatter()
  5. let dateFormatter = DateFormatter()
  6. var calendar = Calendar.current
  7. var Dates: [String] = [];
  8. var Hours: [String] = [];
  9. var todayHours: [String] = [];
  10. let ASAPMessage = "Around 30 Minutes!";
  11. init(){
  12. createDates()
  13. createHours()
  14. createTodayHours()
  15. }
  16.  
  17. func hourToString(){
  18. //let d = hourFormatter.date(from: "8:30 Pm")
  19.  
  20. // endHour = calendar.date(byAdding: .minute, value: 30, to: startHour)!
  21. }
  22. func createTodayHours(){
  23.  
  24. hourFormatter.dateFormat = "h:mm a"
  25. hourFormatter.timeZone = TimeZone(identifier: "PST")
  26. var calendar = Calendar.current
  27. calendar.timeZone = TimeZone(identifier: "PST")!
  28.  
  29. var today = Date()
  30.  
  31. let currentMinute = calendar.component(.minute, from: today)
  32.  
  33. if currentMinute == 0 {
  34. today = calendar.date(byAdding: .minute, value: 30, to: today)!
  35. }else if 30-currentMinute > 0 {
  36. today = calendar.date(byAdding: .minute, value: 60 - currentMinute, to: today)!
  37. }else{
  38. //why 90? lets say the current time is 10:31, if we do 30-31<0, so in order to
  39. //round to make the time at 11:30 we have to do 29+30=59 to get to 11:30. so 90-31=59
  40. today = calendar.date(byAdding: .minute, value: 90 - currentMinute, to: today)!;
  41. }
  42. let indexOfHour = Hours.firstIndex(of: hourFormatter.string(from: today));
  43. todayHours = Array(Hours[indexOfHour!...])
  44. print(todayHours)
  45. }
  46.  
  47. func createHours(){
  48.  
  49. hourFormatter.dateFormat = "h:mm a"
  50. hourFormatter.timeZone = TimeZone(identifier: "UTC")
  51.  
  52. var startHour = hourFormatter.date(from: "12:00 Am")
  53. let endHour = hourFormatter.date(from: "6:00 Pm")
  54.  
  55. while startHour! <= endHour! {
  56. Hours.append(hourToString(hour: startHour!))
  57. startHour = calendar.date(byAdding: .minute, value: 30, to: startHour!)
  58. }
  59. print(Hours)
  60. }
  61.  
  62. func hourToString(hour : Date) -> String {
  63.  
  64. hourFormatter.timeZone = TimeZone(identifier: "UTC")
  65. hourFormatter.dateFormat = "h:mm a"
  66. return hourFormatter.string(from: hour)
  67. }
  68.  
  69. //Dates
  70. func createDates(){
  71. var startDate = Date()
  72. let endDate = calendar.date(byAdding: .day, value: 10, to: startDate)!
  73.  
  74. while startDate <= endDate {
  75. Dates.append(dateToString(day: startDate))
  76. startDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
  77. }
  78. print(Dates)
  79. }
  80. func dateToString(day : Date) -> String {
  81. dateFormatter.dateFormat = "E M/d"
  82. return dateFormatter.string(from: day).capitalized
  83.  
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement