DateExtension
is a little Swift Library for formatting date strings or different dates, such as those returned by (REST) APIs. It can process and automatically recognize different date formats and convert date strings into the desired format.d format.
- Convert
Date
Strings
to user readable formats to use them in aText("")
for example - Convert
Date
Strings
to use them in a func, for example for anAPI
call
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
. - Click on
Add Package
and then again onAdd Package
in the next window.
Then, import the library in your Swift files:
import DateExtension
import 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
.