Skip to content

Instantly share code, notes, and snippets.

@amosgyamfi
Created April 28, 2024 07:15
Show Gist options
  • Save amosgyamfi/08421ffcf3c0b63e9a46f8e2c20bc069 to your computer and use it in GitHub Desktop.
Save amosgyamfi/08421ffcf3c0b63e9a46f8e2c20bc069 to your computer and use it in GitHub Desktop.
//
// DateTimeTimerView..swift
import SwiftUI
struct DateTimeTimerView: View {
var body: some View {
NavigationStack {
List {
// 1. Month, day, year
HStack {
Text("1. Month, day, year:").foregroundStyle(.secondary)
Spacer()
Text(Date.now, format: .dateTime.day().month().year())
}
// 2. Month, day, year: Abreviate date, omit time
HStack {
Text("2. Month, day, year: Abreviate date, omit time:").foregroundStyle(.secondary)
Spacer()
Text((Date.now.formatted(date: .abbreviated, time: .omitted)))
}
// 3. Day, month, year: Long date, omit time
VStack(alignment: .leading) {
HStack {
Text("3. Month, day, year:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(date: .long, time: .omitted))
}
HStack {
// A shorter way
Text("A shorter way").foregroundStyle(.secondary)
Spacer()
Text(Date.now, style: .date)
}
}
// 4. Day, month, year: Complete date, omit time
HStack {
Text("4. Day, month, year - Complete date, omit time:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(date: .complete, time: .omitted))
}
// 5. Day, month, year: Numeric date, omit time
HStack {
Text("5. Day, month, year: Numeric date, omit time:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(date: .numeric, time: .omitted))
}
// 6. Omit date, complete time
HStack {
Text("6. Omit date, complete time:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(date: .omitted, time: .complete))
}
// 7. Omit date, standard time
HStack {
Text("7. Omit date, standard time:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(date: .omitted, time: .standard))
}
// 8. Omit date, shorten time
HStack {
Text("8. Omit date, shorten time:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(date: .omitted, time: .shortened))
}
// 9. Day of the year
VStack(alignment: .leading) {
HStack {
Text("9. Day count from Jan 1st:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(.dateTime.dayOfYear()))
}
HStack {
Text("Today").foregroundStyle(.secondary)
Spacer()
Text(Date().formatted(.dateTime.day()))
}
}
// 10. Era of the year
HStack {
Text("10. Era of the year:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(.dateTime.era()))
}
// 11. Quarter of the year
HStack {
Text("11. Era of the year:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(.dateTime.quarter()))
}
// 12. Week of the year
HStack {
Text("12. Week of the year:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(.dateTime.week()))
}
// 13. Weekday
VStack(alignment: .leading) {
HStack {
Text("13. Weekday:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(.dateTime.weekday()))
}
// Weekday customized
HStack {
Text("Weekday customized").foregroundStyle(.secondary)
Spacer()
Text(Date().formatted(.dateTime.weekday(.wide)))
}
}
// 14. Day & Weekday
HStack {
Text("14. Day & Weekday").foregroundStyle(.secondary)
Spacer()
Text(Date().formatted(.dateTime.day().weekday()))
}
// 15. First letter of a month's name
VStack(alignment: .leading) {
HStack {
Text("15. First letter of a month's name:").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(.dateTime.month(.narrow)))
}
// Month
HStack {
Text("Month").foregroundStyle(.secondary)
Spacer()
Text(Date().formatted(.dateTime.month(.wide)))
}
}
// 16. Year
VStack(alignment: .leading) {
HStack {
Text("16. Year:").foregroundStyle(.secondary)
Spacer()
Text(Date().formatted(.dateTime.year()))
}
// Month
HStack {
Text("Year in 2 digits").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(.dateTime.year(.twoDigits)))
}
}
// 17. AMPMStyle
HStack {
Text("17. AMPMStyle").foregroundStyle(.secondary)
Spacer()
Text(Date.now.formatted(.dateTime.hour(.twoDigits(amPM: .narrow))))
}
// 18. Day, Weekday & Month
HStack {
Text("18. Day, Weekday & Month").foregroundStyle(.secondary)
Spacer()
Text(Date().formatted(.dateTime.day().weekday().month()))
}
// 19. Day, Weekday, Month, and Year
HStack {
Text("19. Day, Weekday, Month, and Year").foregroundStyle(.secondary)
Spacer()
Text(Date().formatted(.dateTime.day().weekday().month().year()))
}
Section {
// 20. Timers
HStack {
Text("20. Time-offset").foregroundStyle(.secondary)
Spacer()
Text(Date.now, style: .offset)
.monospacedDigit()
}
HStack {
Text("21. Relative time").foregroundStyle(.secondary)
Spacer()
Text(Date.now, style: .relative)
.monospacedDigit()
}
HStack {
Text("22. Countup timer:").foregroundStyle(.secondary)
Spacer()
Text(Date.now, style: .timer)
.monospacedDigit()
}
} header: {
Text("Timers")
}
}
.navigationTitle("All about SwiftUI date, time, and timer")
.navigationBarTitleDisplayMode(.inline)
.listStyle(.insetGrouped)
}
}
}
#Preview {
DateTimeTimerView()
.preferredColorScheme(.dark)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment