主页 Swift UNIX C Assembly Go Plan 9 Web MCU Research Non-Tech

How to get data from Date type in SwiftUI

2022-03-04 | Swift | #Words: 188

When we get the time selected by the user in the DatePicker, we will get a Date type data. But this data needs to be used in other data types. Let’s talk about how to achieve this.

The date data type has no related attributes, so there is no way to get what we want simply through .. But Apple provides a function for us to achieve this, as follows:

//Here it means getting the "hour" data, the data type of hour is Int
let hour = Calendar.current.component(.hour, from: Date())
//Here it means getting the data of "minute", the data type of minute is Int
let minute = Calendar.current.component(.minute, from: Date())

You can also get a lot of other data, in addition to time zone, there is also some time-related information. Such as time zone, etc.

​I hope these will help someone in need~