I fail getting 'start' value, tested many variants and formatters. For production reasons cannot use ISO8601DateFormatter.
this code is part of creating appointment on the calendar, so I need start as start date/hour of the appointment.
Above all, I need to understand why this last line of code fails after I tried to convert date:
event.startDate is my 'start' but cannot set it since fails converting date
guard let url = URL(string: "calshow:\(event.startDate.timeIntervalSinceReferenceDate)") else { return }
DispatchQueue.main.async {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
my code:
date0
is a Date I get from API, with value 2021-07-28 00:00:00 +0000
my updated code for creating event on calendar
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let date = dateFormatter.string(from: date0)
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
}
concats code
func concats(_ string: String, withSeparator separator: String? = "") -> String? {
return [self, string].compactMap{ $0 }.joined(separator: separator! + " ")
}
ShortDateTimeFormatter
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")
}
}
If I understand the code you get a Date without the time portion and you want to add the time in string format "HH:mm:ss".
Calendar can do that. You don't need to convert the Date to String and back to Date.
First create a function which converts the time string to Int components
func timeComponents(from string: String) -> (Int, Int, Int)? {
let comps = string.components(separatedBy: ":")
guard comps.count == 3,
let hr = Int(comps[0]),
let mn = Int(comps[1]),
let sc = Int(comps[2])
else { return nil }
return (hr, mn, sc)
}
Then add the time to date0 with date(bySettingHour:minute:second:of:)
if let (hr, mn, sc) = timeComponents(from: "08:30:00") {
let startDate = Calendar.current.date(bySettingHour: hr, minute: mn, second: sc, of: date0)
}
Or with DateComponents
func timeComponents(from string: String) -> DateComponents? {
let comps = string.components(separatedBy: ":")
guard comps.count == 3,
let hr = Int(comps[0]),
let mn = Int(comps[1]),
let sc = Int(comps[2])
else { return nil }
return DateComponents(hour: hr, minute: mn, second: sc)
}
if let components = timeComponents(from: "08:30:00") {
let startDate = Calendar.current.date(byAdding: components, to: date0, wrappingComponents: false)
}
Related
On my project I need to work with hours and min.
I can't find to much info online.
here my issue:
let time1 = "22:00"
let time2 = "20:00"
Question 1:
how do I subtract the time1 - time2 ?
I'm expecting result = 02:00 h
i start writing some code, converting this time to date..
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
var time1d = formatter.date(from: time1)!
let time2d = formatter.date(from: time2)!
but now how do I subtract this two hours?
Question 2:
let time1 = "22:00"
let time2 = "20:00"
How do i sum time1 + time2 ? for example should give me result 42:00 hours
thanks for the help
This is not a Date. If you are only gonna work with hours and minutes and the input will always be a string properly formatted you should struct your data.
Create a Time structure
struct Time {
let hour: Int
let minute: Int
}
And a custom initializer. This assumes your string is always properly formatted 00:00:
extension Time {
init?(string: String) {
guard string.count == 5,
Array(string)[2] == ":",
let hour = Int(string.prefix(2)),
let minute = Int(string.suffix(2)),
0...59 ~= minute else {
return nil
}
self.hour = hour
self.minute = minute
}
}
For displaying your Time struct property you can conform it to CustomStringConvertible and provide a custom description
extension Time: CustomStringConvertible {
var description: String {
String(format: "%02d:%02d", hour, minute)
}
}
Regarding adding and subtracting you can make your Time struct conform to AdditiveArithmetic and implement the required operators:
extension Time: AdditiveArithmetic {
static func - (lhs: Time, rhs: Time) -> Time {
let minutes = lhs.minute - rhs.minute + lhs.hour * 60 - rhs.hour * 60
return .init(hour: minutes/60, minute: minutes%60)
}
static func + (lhs: Time, rhs: Time) -> Time {
let minutes = lhs.minute + rhs.minute + lhs.hour * 60 + rhs.hour * 60
return .init(hour: minutes/60, minute: minutes%60)
}
static var zero: Time { .init(hour: 0, minute: 0) }
}
Playground testing:
let time1 = Time(string: "22:00")!
let time2 = Time(string: "20:00")!
let time3 = time1-time2
print(time3)
let time4 = time1+time2
print(time4)
Those will print
02:00
42:00
First we create the calculateDifference function which uses dateComponents function provided by Swift.
let time1 = "22:00"
let time2 = "20:00"
func formattedTime(_ time: String) -> Date? {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "HH:mm"
return formatter.date(from: time)
}
func calculateDifference(_ from: String, _ to: String) -> (hour: Int, minutes: Int) {
guard let fromTime = formattedTime(from),
let toTime = formattedTime(to) else {
return (0,0)
}
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute], from: fromTime, to: toTime)
return (components.hour ?? 0, components.minute ?? 0)
}
Then we create the calculateSum which uses the calculateDifference internally to get the hours by supplying the default hour as 00:00
func calculateSum(_ time1: String, _ time2: String) -> (hour: Int, minutes: Int) {
let defaultTime = "00:00"
let calculatedTime1 = calculateDifference(defaultTime, time1)
let calcaultedTime2 = calculateDifference(defaultTime, time2)
return(calculatedTime1.hour + calcaultedTime2.hour,
calculatedTime1.minutes + calcaultedTime2.minutes)
}
Now if we run the below, we get the required results. Of course, some formatting will be required.
let difference = calculateDifference(time2, time1)
print("\(difference.hour):\(difference.minutes)")
let sum = calculateSum(time1, time2)
print("\(sum.hour):\(sum.minutes)")
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.
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)
}
I have json from rest API :
{
"status": "ok",
"query": {
"format": "json",
"kota": "703",
"tanggal": "2017-02-07"
},
"jadwal": {
"status": "ok",
"data": {
"ashar": "15:26",
"dhuha": "06:21",
"dzuhur": "12:10",
"imsak": "04:28",
"isya": "19:31",
"maghrib": "18:20",
"subuh": "04:38",
"tanggal": "Selasa, 07 Feb 2017",
"terbit": "05:54"
}
}
}
I want convert jadwal -> data -> ashar and other into Date format
I have this code,
extension String {
func convertToDate() -> Date? {
let arr = self.split(separator: ":")
guard
let hour = Int(arr.first ?? ""),
let minute = Int(arr.last ?? "")
else { return nil }
let component = DateComponents(hour: hour, minute: minute)
var cal = Calendar.current
guard let timezone = TimeZone(identifier: "Asia/Jakarta") else { return nil }
cal.timeZone = timezone
let date = cal.date(from: component)
return date
}
}
But i get this result on my console debug
ashar: 0001-01-01 08:27:48 +0000
i expect for example on asr/ashar = 2021-01-12 15:26:00 for Indonesia Time, do you guys have advice or something?
You can create an extension for string that returns a full date object by passing only the time string as follows.
extension String {
func createDateObjectWithTime(format: String = "HH:mm") -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
guard let dateObjectWithTime = dateFormatter.date(from: self) else { return nil }
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
let components: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
var dateComponents = gregorian.dateComponents(components, from: now)
let calendar = Calendar.current
dateComponents.hour = calendar.component(.hour, from: dateObjectWithTime)
dateComponents.minute = calendar.component(.minute, from: dateObjectWithTime)
dateComponents.second = 0
return gregorian.date(from: dateComponents)
}
}
You can call the extension by:
let date = "15:26".createDateObjectWithTime()
Just get the current date and set hour and minute to the extracted values
extension String {
func convertToDate() -> Date? {
let arr = self.split(separator: ":")
guard
let hour = Int(arr.first ?? ""),
let minute = Int(arr.last ?? "")
else { return nil }
var cal = Calendar.current
guard let timezone = TimeZone(identifier: "Asia/Jakarta") else { return nil }
cal.timeZone = timezone
return cal.date(bySettingHour: hour, minute: minute, second: 0, of: Date())
}
}
iOS Date() returns date with at least microsecond precision.
I checked this statement by calling Date().timeIntervalSince1970 which results in 1490891661.074981
Then I need to convert date into string with microsecond precision.
I am using DateFormatter in following way:
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ"
print(formatter.string(from: date))
which results in
"2017-03-30T16:34:21.075000Z"
Now if we compare two results:
1490891661.074981 and "2017-03-30T16:34:21.075000Z"
we can notice that DateFormatter rounds date to millisecond precision while still presenting zeros for microseconds.
Does anybody know how to configure DateFormatter so I can keep microseconds and get correct result: "2017-03-30T16:34:21.074981Z"?
Thanks to #MartinR for solving first half of my problem and to #ForestKunecke for giving me tips how to solve second half of the problem.
Based on their help I created ready to use solution which converts date from string and vice versa with microsecond precision:
public final class MicrosecondPrecisionDateFormatter: DateFormatter {
private let microsecondsPrefix = "."
override public init() {
super.init()
locale = Locale(identifier: "en_US_POSIX")
timeZone = TimeZone(secondsFromGMT: 0)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func string(from date: Date) -> String {
dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let components = calendar.dateComponents(Set([Calendar.Component.nanosecond]), from: date)
let nanosecondsInMicrosecond = Double(1000)
let microseconds = lrint(Double(components.nanosecond!) / nanosecondsInMicrosecond)
// Subtract nanoseconds from date to ensure string(from: Date) doesn't attempt faulty rounding.
let updatedDate = calendar.date(byAdding: .nanosecond, value: -(components.nanosecond!), to: date)!
let dateTimeString = super.string(from: updatedDate)
let string = String(format: "%#.%06ldZ",
dateTimeString,
microseconds)
return string
}
override public func date(from string: String) -> Date? {
dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
guard let microsecondsPrefixRange = string.range(of: microsecondsPrefix) else { return nil }
let microsecondsWithTimeZoneString = String(string.suffix(from: microsecondsPrefixRange.upperBound))
let nonDigitsCharacterSet = CharacterSet.decimalDigits.inverted
guard let timeZoneRangePrefixRange = microsecondsWithTimeZoneString.rangeOfCharacter(from: nonDigitsCharacterSet) else { return nil }
let microsecondsString = String(microsecondsWithTimeZoneString.prefix(upTo: timeZoneRangePrefixRange.lowerBound))
guard let microsecondsCount = Double(microsecondsString) else { return nil }
let dateStringExludingMicroseconds = string
.replacingOccurrences(of: microsecondsString, with: "")
.replacingOccurrences(of: microsecondsPrefix, with: "")
guard let date = super.date(from: dateStringExludingMicroseconds) else { return nil }
let microsecondsInSecond = Double(1000000)
let dateWithMicroseconds = date + microsecondsCount / microsecondsInSecond
return dateWithMicroseconds
}
}
Usage:
let formatter = MicrosecondPrecisionDateFormatter()
let date = Date(timeIntervalSince1970: 1490891661.074981)
let formattedString = formatter.string(from: date) // 2017-03-30T16:34:21.074981Z
The resolution of (NS)DateFormatter is limited to milliseconds, compare
NSDateFormatter milliseconds bug. A possible solution is to retrieve all date components (up to
nanoseconds) as numbers and do a custom string formatting. The date formatter can still be used for the timezone string.
Example:
let date = Date(timeIntervalSince1970: 1490891661.074981)
let formatter = DateFormatter()
formatter.dateFormat = "ZZZZZ"
let tzString = formatter.string(from: date)
let cal = Calendar.current
let comps = cal.dateComponents([.year, .month, .day, .hour, .minute, .second, .nanosecond],
from: date)
let microSeconds = lrint(Double(comps.nanosecond!)/1000) // Divide by 1000 and round
let formatted = String(format: "%04ld-%02ld-%02ldT%02ld:%02ld:%02ld.%06ld",
comps.year!, comps.month!, comps.day!,
comps.hour!, comps.minute!, comps.second!,
microSeconds) + tzString
print(formatted) // 2017-03-30T18:34:21.074981+02:00
Solution by #Vlad Papko has some issue:
For dates like following:
2019-02-01T00:01:54.3684Z
it can make string with extra zero:
2019-02-01T00:01:54.03684Z
Here is fixed solution, it's ugly, but works without issues:
override public func string(from date: Date) -> String {
dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let components = calendar.dateComponents(Set([Calendar.Component.nanosecond]), from: date)
let nanosecondsInMicrosecond = Double(1000)
let microseconds = lrint(Double(components.nanosecond!) / nanosecondsInMicrosecond)
// Subtract nanoseconds from date to ensure string(from: Date) doesn't attempt faulty rounding.
let updatedDate = calendar.date(byAdding: .nanosecond, value: -(components.nanosecond!), to: date)!
let dateTimeString = super.string(from: updatedDate)
let stingWithMicroseconds = "\(date.timeIntervalSinceReferenceDate)"
let dotIndex = stingWithMicroseconds.lastIndex(of: ".")!
let hasZero = stingWithMicroseconds[stingWithMicroseconds.index(after: dotIndex)] == "0"
let format = hasZero ? "%#.%06ldZ" : "%#.%6ldZ"
let string = String(format: format,
dateTimeString,
microseconds)
return string
}
It is a bit of a hack, but not that complex and 100% Swift:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.'MICROS'xx"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
// Get the number of microseconds with a precision of 6 digits
let now = Date()
let dateParts = Calendar.current.dateComponents([.nanosecond], from: now)
let microSeconds = Int((Double(dateParts.nanosecond!) / 1000).rounded(.toNearestOrEven))
let microSecPart = String(microSeconds).padding(toLength: 6, withPad: "0", startingAt: 0)
// Format the date and add in the microseconds
var timestamp = dateFormatter.string(from: now)
timestamp = timestamp.replacingOccurrences(of: "MICROS", with: microSecPart)
I have a function to work out the start and end of a week which works as expected. I want to implement another function which works out the start and end of a single day. I have the code below however I get the following error:
Type of expression is ambiguous without more context.
public class Date {
let dateFormatter = NSDateFormatter()
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
func calcStartAndEndDateForWeek(durationOccurance: Double) {
print("Calculating start and end for week")
let componentsWeek = calendar.components([.YearForWeekOfYear, .WeekOfYear], fromDate: date)
let startOfWeek = calendar.dateFromComponents(componentsWeek)!
print("start of Week = \(dateFormatter.stringFromDate(startOfWeek))")
let componentsWeekEnds = NSDateComponents()
componentsWeekEnds.weekOfYear = 1
let endOfWeek = calendar.dateByAddingComponents(componentsWeekEnds, toDate: startOfWeek, options: [])!
print("End of the week = \(dateFormatter.stringFromDate(endOfWeek))")
}
func calcStartAndEndDateForDay(durationOccurance: Double) {
print("Calculating start and end for day")
let componentsWeek = calendar.components([.Minutes, .Seconds], fromDate: date)
let startOfDay = calendar.dateFromComponents(componentsWeek)!
print("start day = \(dateFormatter.stringFromDate(startOfDay))")
}
init(){
dateFormatter.dateFormat = "dd-MM-yyyy"
}
}
We can create a more generic function using the methods on NSCalendar:
func rangeOfPeriod(period: NSCalendarUnit, date: NSDate) -> (NSDate, NSDate) {
let calendar = NSCalendar.currentCalendar()
var startDate: NSDate? = nil
// let's ask calendar for the start of the period
calendar.rangeOfUnit(period, startDate: &startDate, interval: nil, forDate: date)
// end of this period is the start of the next period
let endDate = calendar.dateByAddingUnit(period, value: 1, toDate: startDate!, options: [])
// you can subtract 1 second if you want to make "Feb 1 00:00:00" into "Jan 31 23:59:59"
// let endDate2 = calendar.dateByAddingUnit(.Second, value: -1, toDate: endDate!, options: [])
return (startDate!, endDate!)
}
Called as
print("\(rangeOfPeriod(.WeekOfYear, date: NSDate()))")
print("\(rangeOfPeriod(.Day, date: NSDate()))")
Putting it into your code:
public class Date {
let dateFormatter = NSDateFormatter()
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
func rangeOfPeriod(period: NSCalendarUnit) -> (NSDate, NSDate) {
var startDate: NSDate? = nil
calendar.rangeOfUnit(period, startDate: &startDate, interval: nil, forDate: date)
let endDate = calendar.dateByAddingUnit(period, value: 1, toDate: startDate!, options: [])
return (startDate!, endDate!)
}
func calcStartAndEndDateForWeek() {
let (startOfWeek, endOfWeek) = rangeOfPeriod(.WeekOfYear)
print("Start of week = \(dateFormatter.stringFromDate(startOfWeek))")
print("End of the week = \(dateFormatter.stringFromDate(endOfWeek))")
}
func calcStartAndEndDateForDay() {
let (startOfDay, endOfDay) = rangeOfPeriod(.Day)
print("Start of day = \(dateFormatter.stringFromDate(startOfDay))")
print("End of the day = \(dateFormatter.stringFromDate(endOfDay))")
}
init() {
dateFormatter.dateFormat = "dd-MM-yyyy"
}
}
let myDate = Date()
myDate.calcStartAndEndDateForWeek()
myDate.calcStartAndEndDateForDay()
I was implementing something similar and went the following route:
extension Date {
static var startOfToday: Date? {
let date = Date()
guard !date.isStartOfDay else { return date }
return date
.zero(out: .second)?
.zero(out: .minute)?
.zero(out: .hour)?
.addingTimeInterval(-24 * 60 * 60)
}
private func zero(out: Calendar.Component) -> Date? {
return Calendar.current
.date(bySetting: out, value: 0, of: self)
}
private var isStartOfDay: Bool {
let cal = Calendar.current
let hours = cal.component(.hour, from: self)
let minutes = cal.component(.minute, from: self)
let seconds = cal.component(.second, from: self)
return hours == 0 && minutes == 0 && seconds == 0
}
}
Setting a component to zero will increment the next bigger component. So just setting the hour to zero will push the date to the next day at 00:00, unless of course the hour is already at zero. So to make it work for any date we have to zero out the seconds, minutes and hours (in that order). And to make sure we don't end up at the beginning of yesterday we first check if all values aren't already at zero.
I realize this is kinda hacky and probably not the best way to go about this, but it seems to work well enough for my use-case at least.
Getting the end of the day can be built on top of this by just adding another day.