THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial explains getting the date and time in local, UTC, and timezone in swift
First, get the Current Date.
Next, pass the current date Calendar. date function with byAdding=.day
and value is one day.
-1 day is used to pass
Here is an example
import Foundation;
let todayDate = Date()
var tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: todayDate)!
var yesterday = Calendar.current.date(byAdding: .day, value: -1, to: todayDate)!
// Current Date
print(todayDate)
// Tomorrow's Date
print(tomorrow)
// Yesterday's Date
print(yesterday)
Output:
2022-10-30 13:56:10 +0000
2022-09-08 13:56:10 +0000
2022-09-06 13:56:10 +0000
You can also write an extension to the Date of class
Date class added two extension methods.
import Foundation;
extension Date {
var tomorrow: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: self)!
}
var yesterday: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: self)!
}
}
let todayDate = Date()
// Current Date
print(todayDate)
// Tomorrow's Date
print(todayDate.tomorrow)
// Yesterday's Date
print(todayDate.yesterday)
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts