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
Learn Golang Tutorials - Rune Types Explained with examples How to display images in Hugo with examples | Hugo Image shortcode Nodejs package.json resolutions How to find Operating System username in NodeJS? How to convert Double to Integer or Integer to double in Dart| Flutter By ExampleRelated posts