Swift Dates and Times

Swift Datetime

From Jpmtech

I need to develop a calendar module, so I have collected some information online for reference.

Preview

Demo

Code:

Demo Time: 2023/12/23 21:57:54

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
VStack {  
    // print: Dec 23, 2023
    Text(Date.now, format: .dateTime.month().day().year())
    
    // print: Dec 23, 2023 -> (short: Dec)
    Text(Date.now.formatted(date: .abbreviated, time: .omitted))
    
    // print: December 23, 2023 (long: December)
    Text(Date.now.formatted(date: .long, time: .omitted))
    
    // print: Saturday, December 23, 2023 -> long: December, with the weekday
    Text(Date.now.formatted(date: .complete, time: .omitted))
    
    // print: 2023/12/23 (show as the number)
    Text(Date.now.formatted(date: .numeric, time: .omitted))
    
    // print: AD(Anno Domini ...0...2023)
    Text(Date.now.formatted(.dateTime.era()))
    
    // print: Q4 (spring, summer, autumn, winter)
    Text(Date.now.formatted(.dateTime.quarter()))
    
    // print: Weekday: Sat
    Text("Weekday: " + Date.now.formatted(.dateTime.weekday()))
    
    // print: Two Digits year: 23 
    Text("Two Digits year: " + Date.now.formatted(.dateTime.year(.twoDigits)))
    // print: Digits year: 2023
    Text("Digits year: " + Date.now.formatted(.dateTime.year()))
    
    // Demo Time: 2023/12/23 21:57:54
    // print: Month: 12 
    Text("Month: " + Date.now.formatted(.dateTime.month(.twoDigits)))
    // print: Month: Dec
    Text("Month: " + Date.now.formatted(.dateTime.month(.abbreviated)))
    
    // print: Two Digits day: 23
    Text("Two Digits day: " + Date.now.formatted(.dateTime.day(.twoDigits)))
    
    // print: Oclock: 21
    Text("Oclock: " + Date.now.formatted(.dateTime.hour(.twoDigits(amPM: .wide))))
    
    // print: Minutes: 57
    Text("Minutes: " + Date.now.formatted(.dateTime.minute()))
    
    // print: Seconds: 54
    Text("Seconds: " + Date.now.formatted(.dateTime.second()))
    
    // print: 21:57:54 GMT+1 (hour:min:sec GMT+1, Germany)
    Text(Date.now.formatted(date: .omitted, time: .complete))
    
    // print: 21:57:54 (hour:min:sec)
    Text(Date.now.formatted(date: .omitted, time: .standard))
    
    // print: 21:57
    Text(Date.now.formatted(date: .omitted, time: .shortened))
    
    // how many days from the first day of this year
    // print: The day from first: 357
    Text("The day from first: " + Date.now.formatted(.dateTime.dayOfYear()))
    
    // how many weeks from the first day of this year
    // print: The week fromo first day: 51
    Text("The week fromo first day: " + Date.now.formatted(.dateTime.week()))
    
    // the first letter of the month
    // J: January, February: F,..., December: D
    // print: First letter of the Month: D
    Text("First letter of the Month: " + Date.now.formatted(.dateTime.month(.narrow)))
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy