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)")
Related
I'm looking for cool ways to stride through Date ranges with different increments (either seconds aka TimeInterval or with DateComponents aka .hour, .minute)
import Foundation
extension Date: Strideable {
// typealias Stride = SignedInteger // doesn't work (probably because declared in extension
public func advanced(by n: Int) -> Date {
self.addingTimeInterval(TimeInterval(n))
}
public func distance(to other: Date) -> Int {
return Int(self.distance(to: other))
}
}
let now = Date()
let dayAfterNow = Date().addingTimeInterval(86400)
let dateRange = now ... dayAfterNow
let dateArr : [Date] = Array(stride(from: now, to: dayAfterNow, by: 60)) // Solves my task but not exactly how I wanted.
let formatter: DateFormatter = {
let df = DateFormatter()
df.timeStyle = .short
return df }()
print (dateArr.prefix(7).map { formatter.string(from: $0) })
/// This hoever doesn't work
// There must be a way to make it work but couldn't figure it out for now
let errDateArr: [Date] = Array(dateRange)
// Error: Initializer 'init(_:)' requires that 'Date.Stride' (aka 'Double') conform to 'SignedInteger'
The second part of the question is that also i'd like to have something like:
var components = DateComponents()
components.hour = 8
components.minute = 0
let date = Calendar.current.date(from: components)
let dateByComponentArr : [Date] = Array(stride(from: now, to: dayAfterNow, by: components))
As already mentionerd by #Alexander you shouldn't try to conform Date to Strideable but you can implement your own methods to generate the next n days, hours or minutes:
extension Date {
func year(using calendar: Calendar = .current) -> Int { calendar.component(.year, from: self) }
func month(using calendar: Calendar = .current) -> Int { calendar.component(.month, from: self) }
func day(using calendar: Calendar = .current) -> Int { calendar.component(.day, from: self) }
func hour(using calendar: Calendar = .current) -> Int { calendar.component(.hour, from: self) }
func minute(using calendar: Calendar = .current) -> Int { calendar.component(.minute, from: self) }
func nextDays(n: Int, nth: Int = 1, using calendar: Calendar = .current) -> [Date] {
let year = self.year(using: calendar)
let month = self.month(using: calendar)
let day = self.day(using: calendar)
var days: [Date] = []
for x in 0..<n where x.isMultiple(of: nth) {
days.append(DateComponents(calendar: calendar, year: year, month: month, day: day + x, hour: 12).date!)
}
return days
}
func nextHours(n: Int, nth: Int = 1, using calendar: Calendar = .current) -> [Date] {
let year = self.year(using: calendar)
let month = self.month(using: calendar)
let day = self.day(using: calendar)
let hour = self.hour(using: calendar)
var hours: [Date] = []
for x in 0..<n where x.isMultiple(of: nth) {
hours.append(DateComponents(calendar: calendar, year: year, month: month, day: day, hour: hour + x).date!)
}
return hours
}
func nextMinutes(n: Int, nth: Int = 1, using calendar: Calendar = .current) -> [Date] {
let year = self.year(using: calendar)
let month = self.month(using: calendar)
let day = self.day(using: calendar)
let hour = self.hour(using: calendar)
let minute = self.minute(using: calendar)
var minutes: [Date] = []
for x in 0..<n where x.isMultiple(of: nth) {
minutes.append(DateComponents(calendar: calendar, year: year, month: month, day: day, hour: hour, minute: minute + x).date!)
}
return minutes
}
}
extension Date {
static let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
return formatter
}()
}
Playgournd testing:
let days = Date().nextDays(n: 10)
for day in days {
print(Date.formatter.string(from: day))
}
let hours = Date().nextHours(n: 10)
for hour in hours {
print(Date.formatter.string(from: hour))
}
let minutes = Date().nextMinutes(n: 10)
for minute in minutes {
print(Date.formatter.string(from: minute))
}
There's a conflicting implementation of distance(to:) already declared on Date: Date.distance(to:). The implementation you defined has the same name, but a different type. This other overload could work, but unfortunately for you, Date already declares a typealias called Stride, which it sets to TimeInterval (just an alias for Double).
I think conforming to Date to Strideable is a bad idea. Take Double for example, which intentionally doesn't conform to Strideable: there's no clear universally-best stride definition. Should it go up by 0.1? 0.01? 3.14? It's not obvious. stride(from:to:by:) exists precisely for this reason, for you to be explicit by how much you're striding by.
let errDateArr: [Date] = Array(now ... dayAfterNow) would definitely score high "WTFs/m" points
For the second part of the question I created a simple sequence that takes daylight saving settings and other stuff in account. It might be helpful in some specific cases.
let dayAfterNow = Date().addingTimeInterval(86400)
var components = DateComponents()
components.hour = 8
components.minute = 0
func dateIterator(start: Date = Date(), by components: DateComponents, wrappingComponents: Bool = false) -> AnyIterator<Date> {
var state = start
return AnyIterator {
let nextDate = Calendar.current.date(byAdding: components, to: state, wrappingComponents: wrappingComponents)
state = nextDate ?? state
return state
}
}
let dateCompSequence = AnySequence(dateIterator(by: components))
let dateArray = Array(dateCompSequence.prefix(10))
dateArray.map{print($0.description)}
print("starting for loop...")
for d in dateCompSequence.prefix(10) {
print(d.description)
}
It is worth noting that Calendar.current.enumerateDates() will likely always help with similar tasks. But having a sequence is sometimes preferable. In earlier version of Swift there seemed to be a strideable DateRange type provided by NSCalendar (just the thing I wanted), but now there is nothing like it in standard library.
I need to check a date before downloading / manipulate some data from a server. Let's say I need to do that only if 24 hours or more are gone by. this code seems to work, but I'm not sure about it, no way to do it with less lines of code? it seems to be too long to me. i checked this but solutions are quite different from mine.
import UIKit
//standard day formatter
let dateFormatter = DateFormatter()
//let's say this is the date I saved last time i updated data from online server:
let previousDate: String = "2019-03-19 06:40 PM"
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
let old = dateFormatter.date(from: previousDate)
//today I try to download data
let today = Date()
//this simply tests if "moment before" is greater than "moment after"
if today > old! {
print("update data")
} else {
print("do not update")
}
//here I create a measure
let minute:TimeInterval = 60.0
let hour:TimeInterval = 60.0 * minute
let day:TimeInterval = 24 * hour
//here I measure if the old date added of 24h is greater than now, in that case a day or more is passed and I can update
let theOldDateMore24h = Date(timeInterval: day, since: old!)
if theOldDateMore24h < today {
print("passed more than a day: Update!")
} else {
print("less than a day, do not update")
}
There is a method in Calendar
func dateComponents(_ components: Set<Calendar.Component>, from start: Date, to end: Date) -> DateComponents
Get the day component and check greater than 0
let dateFormatter = DateFormatter()
let previousDate = "2019-03-19 06:40 PM"
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
let old = dateFormatter.date(from: previousDate)
//today I try to download data
let today = Date()
if let validDate = old, Calendar.current.dateComponents([.day], from: validDate, to: today).day! > 0 {
print("passed more than a day: Update!")
} else {
print("less than a day, do not update")
}
Quick extension function to simplify it:
extension Date {
func isWithin(_ distanceTime: TimeInterval, after laterDate: Date) -> Bool{
let distance = timeIntervalSince(laterDate)
let result = distanceTime >= distance
return result
}
}
//Usage
let secondsInDay = TimeInterval(60 * 60 * 24)
let isUpToDate = Date().isWithin(secondsInDay, after: previousDate)
if !isUpToDate {
print("passed more than a day: Update!")
}
else {
print("less than a day, do not update")
}
You can actually use an extension for this. It will return the required calendar component
Extension
extension Date {
func interval(ofComponent comp: Calendar.Component, fromDate date: Date) -> Int {
let currentCalendar = Calendar.current
guard let start = currentCalendar.ordinality(of: comp, in: .era, for: date) else { return 0 }
guard let end = currentCalendar.ordinality(of: comp, in: .era, for: self) else { return 0 }
return end - start
}
}
Usage
let yesterday = Date(timeInterval: -86400, since: Date())
let tomorrow = Date(timeInterval: 86400, since: Date())
// Change the component to your preference
let difference = tomorrow.interval(ofComponent: .day, fromDate: yesterday) // returns 2
I am trying to convert the timestamp from server and it is converting also but only month is coming wrong.Like timestamp is 1492747393892 and it convert into 21/03/17 - 12:03PM but it should be 21/04/17 - 12:03PM.
Here is my code
var arriveTimestamp: Int
if let timeStampToDate = arriveTimestamp {
let timeSt = Date(jsonTimeDate:"/Date(\(timeStampToDate))/")
let time = Date().dateTime(date: timeSt!)
valueLbl.text = time
}
init?(jsonTimeDate: String) {
// "/Date(1487058855745)/"
let prefix = "/Date("
let suffix = ")/"
let scanner = Scanner(string: jsonTimeDate)
// Check prefix:
guard scanner.scanString(prefix, into: nil) else { return nil }
// Read milliseconds part:
var milliseconds : Int64 = 0
guard scanner.scanInt64(&milliseconds) else { return nil }
// Milliseconds to seconds:
var timeStamp = TimeInterval(milliseconds)/1000.0
// Read optional timezone part:
var timeZoneOffset : Int = 0
if scanner.scanInt(&timeZoneOffset) {
let hours = timeZoneOffset / 100
let minutes = timeZoneOffset % 100
// Adjust timestamp according to timezone:
timeStamp += TimeInterval(3600 * hours + 60 * minutes)
}
// Check suffix:
guard scanner.scanString(suffix, into: nil) else { return nil }
// Success! Create NSDate and return.
self.init(timeIntervalSince1970: timeStamp)
}
func dateTime(date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/mm/yy - hh:mm a"
return dateFormatter.string(from: date as Date)
}
The main error in your code is the wrong date format for the month,
which should be "MM", not "mm" (which is for the minutes).
Apart from that, your approach is far too complicated. All you have to
do is to divide the timestamp (which is in milliseconds) by 1000
and call Date(timeIntervalSince1970:):
let arriveTimestamp = 1492747393892
let date = Date(timeIntervalSince1970: TimeInterval(arriveTimestamp)/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yy - hh:mm a"
let text = dateFormatter.string(from: date)
print(text) // 21/04/17 - 06:03 AM
I can get the offset seconds from GMT with this: TimeZone.current.secondsFromGMT().
However, how do I get the format as ±hh:mm?
Some integer arithmetic to obtain the offset in hours and
minutes:
let seconds = TimeZone.current.secondsFromGMT()
let hours = seconds/3600
let minutes = abs(seconds/60) % 60
Formatted printing:
let tz = String(format: "%+.2d:%.2d", hours, minutes)
print(tz) // "+01:00"
%.2d prints an integer with (at least) two decimal digits (and leading
zero if necessary). %+.2d is the same but with a leading + sign for
non-negative numbers.
Here is extension for getting timezone offset Difference and as ±hh:mm (Swift 4 | Swift 5 Code)
extension TimeZone {
func offsetFromUTC() -> String
{
let localTimeZoneFormatter = DateFormatter()
localTimeZoneFormatter.timeZone = self
localTimeZoneFormatter.dateFormat = "Z"
return localTimeZoneFormatter.string(from: Date())
}
func offsetInHours() -> String
{
let hours = secondsFromGMT()/3600
let minutes = abs(secondsFromGMT()/60) % 60
let tz_hr = String(format: "%+.2d:%.2d", hours, minutes) // "+hh:mm"
return tz_hr
}
}
Use like this
print(TimeZone.current.offsetFromUTC()) // output is +0530
print(TimeZone.current.offsetInHours()) // output is "+05:30"
If you can use Date()
func getCurrentTimezone() -> String {
let localTimeZoneFormatter = DateFormatter()
localTimeZoneFormatter.dateFormat = "ZZZZZ"
return localTimeZoneFormatter.string(from: Date())
}
Will return "+01:00" format
extension TimeZone {
func offsetFromUTC() -> String
{
let localTimeZoneFormatter = DateFormatter()
localTimeZoneFormatter.timeZone = self
localTimeZoneFormatter.dateFormat = "Z"
return localTimeZoneFormatter.string(from: Date())
}
func currentTimezoneOffset() -> String {
let timeZoneFormatter = DateFormatter()
timeZoneFormatter.dateFormat = "ZZZZZ"
return timeZoneFormatter.string(from: Date())
}
}
Use like this
print(TimeZone.current.offsetFromUTC()) // output is +0530
print(TimeZone.current.currentTimezoneOffset()) // output is "+05:30"
it working 100% in all countries according to timezone.
Swift 4 and above
extension TimeZone {
func timeZoneOffsetInHours() -> Int {
let seconds = secondsFromGMT()
let hours = seconds/3600
return hours
}
func timeZoneOffsetInMinutes() -> Int {
let seconds = secondsFromGMT()
let minutes = abs(seconds / 60)
return minutes
}
}
The accepted answer does not handle the case "-00:30" correctly since the "+/-" is only being determined from the hours, and not the minutes. I would set the sign based on a check of the initial seconds value. Alternatively you could use DateComponentsFormatter.
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .positional
formatter.zeroFormattingBehavior = .pad
let interval: TimeInterval = TimeInterval.init(abs(secondsOffset))
let offsetValue: String = formatter.string(from: interval)
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.