-2

in a mock json file, I receive separated date, start hour, end hour, so I need to merge date and a start date, then doing the same with end date. Before, I had a date like this:

23/03/2022

and code worked, now I have this, which is causing old code to crash:

"2021-06-25T07:50:56.970Z”

the portion of the object from with I get date and the hours I need

"date": "2021-06-25T07:50:56.970Z",
                      "time": {
                        "from": "17:00",
                        "to": "18:00"
                      }

code in which such date was manipulated:

public class ShortDateTimeFormatter: DateFormatter {
    
    override public init() {
        super.init()
        self.dateFormat = "dd/MM/yyyy HH:mm"
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

used here as let

private let shortTimeFormatter = ShortDateTimeFormatter()
          

called here

 if let startTime = hours.from,
                   let startDate = date.concats(startTime),
                    let start = self.shortTimeFormatter.date(from: startDate) {
                    event.startDate = start
                    event.addAlarm(EKAlarm(absoluteDate: start.dayBefore))
                    event.addAlarm(EKAlarm(absoluteDate: start.halfHourBefore))
                    event.endDate = start.hourAfter
                }
        
            if let endTime = hours.to,
                let endDate = date.concats(endTime),
                let end = self.shortTimeFormatter.date(from: endDate) {
                event.endDate = end
            }

eventually, used here

 guard let url = URL(string: "calshow:\(event.startDate.timeIntervalSinceReferenceDate)") else { return }

how can I put together a date I can use now? in other way, I think I need to get day/month/year from “2021-06-25T07:50:56.970Z” put tighter a format like the "old date" and go on. or maybe a different way to handle it is needed.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
biggreentree
  • 1,633
  • 3
  • 20
  • 35

1 Answers1

0

Here's what you can do.

import Foundation

let input = "2021-06-25T07:50:56.970Z"
let iso8601 = ISO8601DateFormatter()
iso8601.formatOptions = [.withFullDate, .withFullTime, .withFractionalSeconds]
if let date = iso8601.date(from: input) {
    var calendar = Calendar.current
    calendar.timeZone = TimeZone(secondsFromGMT: 0)! // same as your input time zone (and force cast tested only for UTC)
    let dateComps = calendar.dateComponents([.day, .month, .year], from: date)
    
    let start = "17:00"
    let startDate = dateByAppending(time: start, to: dateComps, using: calendar)
    
    let end = "18:00"
    let endDate = dateByAppending(time: end, to: dateComps, using: calendar)
}

func dateByAppending(time: String, to dateComponents: DateComponents, using calendar: Calendar) -> Date? {
    var dateComps = dateComponents
    let timeComps = time.components(separatedBy: ":")
    if timeComps.count == 2 {
        dateComps.hour = Int(timeComps[0])
        dateComps.minute = Int(timeComps[1])
    }
    return calendar.date(from: dateComps)
}
Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30