[Swift] Get the current time numerically

What do you mean

For example, I want to get the numbers 12 and 34 from the time ** 12: 34 **. I've found a lot of using DateFormatter for the first time to get the date and time in Swift, but I couldn't (to me) cast it well because it was a string type. The method I managed to find worked.

Calendar

let now = Date()

let time = Calendar.current.dateComponents([.hour, .minute], from: now)

print(time.hour!) 
print(time.minute!)

I am vaguely wondering if I was spoiled by PHP. Even so, isn't it so troublesome to do such a simple thing? If there is an easy way, please let me know.

Recommended Posts

[Swift] Get the current time numerically
Get the current date and time by specifying the time zone in Thymeleaf
[Java] How to get the current directory
[Swift] Get the height of Safe Area
[Java] How to get the current date and time and specify the display format
[Swift] Get the number of steps with CMP edometer
[Swift] How to get the document ID of Firebase
Now is the time to get started with the Stream API
[Swift] Get the timing when the value of textField is changed
[Swift5] How to get an array and the complement of arrays
[Swift UI] How to get the startup status of the application [iOS]