Time and date formatter SwiftUI JSON data - swift

Hopefully this an easy one. Im trying to display the date and time in a list In an iOS application using SwiftUI. I'm having issues with the code at the moment and getting this error message: Cannot convert value of type 'PostListViewModel' to expected argument type 'Data'
Please find the code below:
struct PostViewModel {
var post: Post
init(post: Post) {
self.post = post
}
var startTime: Date {
let data = PostListViewModel()
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(DateFormatter.DateFormat)
let postDate = try! decoder.decode(startTime.self, from: data)
return self.post.startTime
}
}
extension DateFormatter {
static let DateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MMM d"
formatter.calendar = Calendar(identifier: .iso8601)
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()
}
Can you see any issues with the code?

If you want to convert a date string to a Date and then to another date string use one date formatter with two different date format strings
struct PostViewModel {
var post: Post
init(post: Post) {
self.post = post
}
var startTime: String {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let date = formatter.date(from: post.startTime) {
formatter.dateFormat = "MMM d"
return formatter.string(from: date)
} else {
return post.startTime
}
}
}

Related

Swift - Convert a short format string of a date to long format using date formatter

I have a string that looks like this: "2021-08-05"
I would like to convert it into a date so that I can display it as "5th August 2021"
My code below just returns as nil and I don't understand why. Any help would be appreciated.
Code:
func stringToDate(dateString: String){
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"
formatter.dateStyle = .long
let newDate = formatter.date(from: dateString)
print("formatter: \(newDate)")
}
I call the function as an action for a button (SwiftUI), so when the button is clicked the date should show.
Button("Go"){
stringToDate(dateString: "2021-08-05")
}
Removing the dateStyle line fixes it, and an actual date is printed.
Code:
func stringToDate(dateString: String){
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"
let newDate = formatter.date(from: dateString)
print("formatter: \(newDate)")
}
// Prints: formatter: Optional(2021-08-04 23:00:00 +0000)
To do the whole conversion, the following function will work:
func convertDate(from original: String) -> String? {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"
guard let date = formatter.date(from: original) else { return nil }
formatter.dateStyle = .long
let string = formatter.string(from: date)
return string
}
// Prints: Optional("August 5, 2021")
Of course, this may be printed slightly different due to my location & language
The following line of code can only be run after getting a Date otherwise you are 'overwriting' the dateFormat:
formatter.dateStyle = .long
You're overriding the .dateFotrmat setting with the .dateStyle setting.
If you want to do it this way, use the format string to convert the string to a date, then the style to output it.
func stringToDate(dateString: String){
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"
let newDate = formatter.date(from: dateString)
formatter.dateStyle = .long
print("formatter: \(formatter.string(from: newDate!) )")
}
which give the output you wanted:
formatter: August 5, 2021

How to format date string if the given date format changes

I'm getting a response from api that is date string, it's format changes sometimes so I need to format it dynamically,
Here's my code
import Foundation
func format(from: String?, fromFormat: String, to: String) -> String {
if from == nil { return "" }
let inputDateFormatter = DateFormatter()
inputDateFormatter.dateFormat = fromFormat
let date = inputDateFormatter.date(from: from ?? "")
let outputDateFormatter = DateFormatter()
outputDateFormatter.dateFormat = to
if let date = date {
return outputDateFormatter.string(from: date)
}
return "not formatted"
}
let strFromApi = "2020-12-22"
print(format(from: strFromApi, fromFormat: "yyyy-MM-dd", to: "d MMM yyyy"))
As you can see, I have a code there that can format successfully, the problem here is that strFromApi variable was came from api, and is changing between 2020-12-22 and 2020-12-22 00:00:00, when it changes to 2020-12-22 00:00:00, my current code can't format it anymore.
Question: How can I format it even the given format from the server has time?
You can create two date formatters, one with time and another without it and use nil coalescing operator to provide a fallback in case the first one fails. Regarding the date format when returning your final string you should respect the user's device locale and settings. Just use dateStyle and timeStyle when displaying a date to the user:
extension Formatter {
static let date: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
static let dateAndTime: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter
}()
static let localizedDate: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
return formatter
}()
}
extension Date {
var localizedDate: String { Formatter.localizedDate.string(from: self) }
}
func formatted(from string: String?) -> String {
guard
let string = string,
let date = Formatter.dateAndTime.date(from: string) ??
Formatter.date.date(from: string)
else { return "" }
return date.localizedDate
}
let strFromApi = "2020-12-22 00:00:00" // "2020-12-22" //
formatted(from: strFromApi) // "Dec 22, 2020"

How to convert this string in a date with this format?

Im working with a API, when I ask for a date, this API get my a String like this:
20190717-0300
I want to show that date in a UILabel with this format: "dd '\(preposition)' MMMM"
My attempt was to make a string extension:
extension String {
var toDate: String? {
let dateFormatter = DateFormatter()
let preposition = NSLocalizedString("of", comment: "Preposition of dates formatted")
dateFormatter.dateFormat = "dd '\(preposition)' MMMM"
dateFormatter.locale = Locale.current
if let date = dateFormatter.date(from: self) {
let dateString = dateFormatter.string(from: date)
return dateString
}
return nil
}
}
And then use it:
myLabel.text = thatString.toDate
But .toDate always return nil
Note: The answers I found on the site are cases with ISO format strings.
Expected result:
17 of July
Basically you need an input date format and an output date format.
extension String {
var toDate: String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMddZ"
if let date = dateFormatter.date(from: self) {
let preposition = NSLocalizedString("of", comment: "Preposition of dates formatted")
dateFormatter.dateFormat = "dd '\(preposition)' MMMM"
let dateString = dateFormatter.string(from: date)
return dateString
}
return nil
}
}
I totally agree with rmaddy's comment to use setLocalizedDateFormatFromTemplate
The source of the date field symbols is unicode.org: Date Format Patterns
Turn this into extension if you like:
// function
func formatDate(_ from:String, preposition:String) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMddZ"
guard let date = dateFormatter.date(from: from) else {
return nil
}
dateFormatter.dateFormat = "dd '\(preposition)' MMMM"
return dateFormatter.string(from: date)
}
// example usage
let str = "20190717-0300"
if let formatted = formatDate(str, preposition: "of") {
print(formatted)
}

Parsing a Swift String to Date, then Components

I have a date "2017-12-31" as a String.
What I want to get finally is only the month: "12" as a String.
So I thought that I can change it to Date using a date formatter
let formatter = DateFormatter()
formatter.dateFormat = "MM"
What do I do next?
let dateString = "2017-12-31"
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: Calendar.Identifier.iso8601) formatter.timeZone = TimeZone(identifier: TimeZone.autoupdatingCurrent.identifier)
formatter.dateFormat = "yyyy-MM-dd"
let localDate = formatter.date(from: dateString)
formatter.dateFormat = "MM"
let strMonth = formatter.string(from: localDate!)
print("Month is:",strMonth)
Another way
let dateString = "2017-12-31"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let localDate = formatter.date(from: dateString)
let month = String(NSCalendar.current.component(.month, from: localDate!))
print(month)
First you have to use the DateFormatter to create a temporary Date object from your source String object. Then you have to use it to create your final String from the temporary Date object.
let dateString = "2017-12-31"
let dateFormatter = DateFormatter()
// set the dateFormatter's dateFormat to the dateString's format
dateFormatter.dateFormat = "yyyy-MM-dd"
// create date object
guard let tempDate = dateFormatter.date(from: dateString) else {
fatalError("wrong dateFormat")
}
// set the dateFormatter's dateFormat to the output format you wish to receive
dateFormatter.dateFormat = "LL" // LL is the stand-alone month
let month = dateFormatter.string(from: tempDate)
Use below function for getting month from string file of date
func getMonthFromDateString(strDate: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.date(from: strDate) // Convert String File To Date
formatter.dateFormat = "MM"
let strMM = formatter.string(from: date!) // Convert date to string
return strMM
}

Convert string with timezone to date

I have this type of string and want to convert it to date
"2017-05-27T00:00:00.000+0400"
but none of this formatters convert it to date
"yyyy-MM-dd'T'HH:mm:SSSZ"
"yyyy-MM-dd'T'HH:mm:SSSX"
Your forgot to add ss for seconds so correct formate should be yyyy-MM-dd'T'HH:mm:ss.SSSZ
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: "2017-05-27T00:00:00.000+0400")
Your error was that the seconds were missing. So the right format should be: yyyy-MM-dd'T'HH:mm:ss.SSSZ. You can also use an extension for this purpose:
extension String {
var toCustomDate: Date {
return Date.Formatter.customDate.date(from: self)!
}
}
extension Date {
struct Formatter {
static let customDate: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
return formatter
}()
}
var customDate: String {
return Formatter.customDate.string(from: self)
}
}
let str = "2017-05-27T00:00:00.000+0400"
let date = str.toCustomDate
If you have more date formats, then just add them to the extensions.
you can do like this
let dateString = "2017-05-27T00:00:00.000+0400"
let formachanger = DateFormatter()
formachanger .dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let dateFromString = formachanger .date(from: dateString) {
formachanger .dateFormat = "yyyy-MM-dd HH:mm"
let stringFromDate = formachanger .string(from: dateFromString)
}
try this function
func getGMTDateFrom(String string : String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss zz"
let dateObj = dateFormatter.date(from: string)
return dateObj
}
Change date format according to your need.