Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Created October 8, 2024 14:23
Show Gist options
  • Save lucianoschillagi/98474577d01354ed040ad4721b55c9cd to your computer and use it in GitHub Desktop.
Save lucianoschillagi/98474577d01354ed040ad4721b55c9cd to your computer and use it in GitHub Desktop.
swiftui environment values → calendar (use case)
import SwiftUI
struct CalendarView: View {
@Environment(\.calendar) var calendar // Access the user's calendar environment value
// Computed property to get the current date in the user's calendar
var currentDateInUserCalendar: String {
let formatter = DateFormatter()
formatter.calendar = calendar // Use the user's calendar
formatter.dateStyle = .full
return formatter.string(from: Date())
}
var body: some View {
VStack(spacing: 20) {
Text("Today's Date")
.font(.largeTitle)
// Display the current date formatted according to the user's calendar
Text(currentDateInUserCalendar)
.font(.title)
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment