Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class DateAndTime {
- let hourFormatter = DateFormatter()
- let dateFormatter = DateFormatter()
- var calendar = Calendar.current
- var Dates: [String] = [];
- var Hours: [String] = [];
- var todayHours: [String] = [];
- let ASAPMessage = "Around 30 Minutes!";
- init(){
- createDates()
- createHours()
- createTodayHours()
- }
- func hourToString(){
- //let d = hourFormatter.date(from: "8:30 Pm")
- // endHour = calendar.date(byAdding: .minute, value: 30, to: startHour)!
- }
- func createTodayHours(){
- hourFormatter.dateFormat = "h:mm a"
- hourFormatter.timeZone = TimeZone(identifier: "PST")
- var calendar = Calendar.current
- calendar.timeZone = TimeZone(identifier: "PST")!
- var today = Date()
- let currentMinute = calendar.component(.minute, from: today)
- if currentMinute == 0 {
- today = calendar.date(byAdding: .minute, value: 30, to: today)!
- }else if 30-currentMinute > 0 {
- today = calendar.date(byAdding: .minute, value: 60 - currentMinute, to: today)!
- }else{
- //why 90? lets say the current time is 10:31, if we do 30-31<0, so in order to
- //round to make the time at 11:30 we have to do 29+30=59 to get to 11:30. so 90-31=59
- today = calendar.date(byAdding: .minute, value: 90 - currentMinute, to: today)!;
- }
- let indexOfHour = Hours.firstIndex(of: hourFormatter.string(from: today));
- todayHours = Array(Hours[indexOfHour!...])
- print(todayHours)
- }
- func createHours(){
- hourFormatter.dateFormat = "h:mm a"
- hourFormatter.timeZone = TimeZone(identifier: "UTC")
- var startHour = hourFormatter.date(from: "12:00 Am")
- let endHour = hourFormatter.date(from: "6:00 Pm")
- while startHour! <= endHour! {
- Hours.append(hourToString(hour: startHour!))
- startHour = calendar.date(byAdding: .minute, value: 30, to: startHour!)
- }
- print(Hours)
- }
- func hourToString(hour : Date) -> String {
- hourFormatter.timeZone = TimeZone(identifier: "UTC")
- hourFormatter.dateFormat = "h:mm a"
- return hourFormatter.string(from: hour)
- }
- //Dates
- func createDates(){
- var startDate = Date()
- let endDate = calendar.date(byAdding: .day, value: 10, to: startDate)!
- while startDate <= endDate {
- Dates.append(dateToString(day: startDate))
- startDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
- }
- print(Dates)
- }
- func dateToString(day : Date) -> String {
- dateFormatter.dateFormat = "E M/d"
- return dateFormatter.string(from: day).capitalized
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement