DateExtension is a little Swift Library for formatting date strings or different dates, such as those returned by (REST) APIs in a human readable and type safe Format. It can process and automatically recognize different date formats and convert date strings into the desired format
- Convert
DateStringsto user readable formats to use them in aText("")for example - Convert
DateStringsto use them in a func, for example for anAPIcall
To install this package, follow these steps:
- Go to
General->Frameworks, Libraries, and Embedded Content. - Click on the
+button. - Click on the "Add More" dropdown and select
Add Package Dependency.... - Paste the following URL in the top right:
[https://github.com/johannesxdev/DateExtension.git](https://github.com/johannes-bln/DateExtension). - Click on
Add Packageand then again onAdd Packagein the next window.
Then, import the library in your Swift files:
import DateExtensionimport SwiftUI
import DateExtension
struct ContentView: View {
var body: some View {
let time = "2023-12-31T14:35:50Z"
Text(time.formattedDate(.ddMMyyyy))
}
}In this example, a date in ISO 8601 format Text("2023-12-31T14:35:50Z") within a text is automatically detected and converted into the format dd.MM.yyyy Text("31.12.2023").
import SwiftUI
import DateExtension
struct ContentView: View {
var body: some View {
Button("func"){
fetchFunc()
}
}
func fetchFunc() {
let time = "12:23:00"
let date = "12.09.2024"
let iso8601 = "\(date) \(time)".formattedDate(.iso8601)
print(iso8601)
}
}In this example, a date and time within a text are automatically detected and converted into the ISO 8601 format 2023-12-31T14:35:50Z.