get the start and end of today as an iso string in swift

You can use Swift's Date and Calendar classes to get the start and end of today as an ISO8601 formatted string. Here's how you can do it:

main.swift
let now = Date() // get the current date and time
let calendar = Calendar(identifier: .gregorian) // use the Gregorian calendar
let startOfDay = calendar.startOfDay(for: now) // get the start of today
let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)! // get the start of tomorrow and subtract a second to get the end of today

let formatter = ISO8601DateFormatter() // create an ISO8601 formatter
formatter.timeZone = TimeZone.current // set the formatter's timezone
let startOfDayISO = formatter.string(from: startOfDay) // format the start of today as an ISO string
let endOfDayISO = formatter.string(from: endOfDay.addingTimeInterval(-1)) // format the end of today as an ISO string
708 chars
10 lines

The startOfDayISO and endOfDayISO variables now contain the start and end of today as ISO8601 formatted strings.

gistlibby LogSnag