Swift dateFormat for a dateType string variable

I am trying to parse some json that is the result of an oData connection and I am getting the following response from the server:

"Task_ID":24,"Equipment_ID":3,"Owner_ID":2,"Priority":5,"Date_Due":
"2015-04-08T19:37:56.913","Time_Complete":"2015-04-09T19:37:56","Task_Description"

      

I'm really interested in the two different date fields that I get:

"Date_Due":"2015-04-08T19:37:56.913"
      

and

"Time_Complete":"2015-04-09T19:37:56"

      

As we can see you have a millisecond timestamp and the other does not.

Looking at the DB, this is because the millisecond is actually .000 in the database (MS SQL Server), and for whatever reason, the result I get in my json is truncated by that part.

I'm not interested in milliseconds, but I would like to have a dateFormat function that can handle both scenarios.

Now I have an obvious answer that (pre) parses each date field, removes the milliseconds if any, then use the following code to format them:

let SQLDateFormatter: NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    return formatter
}()

      

However, I would like to know if we can build a formatter that can solve this problem without prior analysis, which would take both:

formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

      

and

formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"

      

and format it automatically.

I checked this issue but couldn't find anything, thanks in advance ...

+3


source to share


2 answers


extension Formatter {
    static let iso8601: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.timeZone = TimeZone(secondsFromGMT: 0)
        return formatter
    }()
    static let iso8601withFractionalSeconds: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.timeZone = TimeZone(secondsFromGMT: 0)
        return formatter
    }()
}

      


extension String {
    var date: Date? {
       return Formatter.iso8601withFractionalSeconds.date(from: self) ??
            Formatter.iso8601.date(from: self)
    }
}

      




"2015-04-08T19:37:56.913".date  // "Apr 8, 2015, 4:37 PM"
"2015-04-09T19:37:56".date      // "Apr 9, 2015, 4:37 PM"

      

+3


source


No, NSDateFormatter

will return zero if the string is not specified in the exact dateFormat as specified.

What can you do instead of preprocessing the string, just check if the string you got is with or without miliseconds.



The best way to do this is using Regex. The idea is to create a SQLDateFormatter like you are normal and then check if the string has milliseconds or not. If milliseconds are included, just change the dateFormat method better than parsing the string.

if let match = tes.rangeOfString("(\\d{4}-\\d\\d-\\d\\d[T](\\d\\d:){2}\\d\\d.\\d{3})", options: NSStringCompareOptions.RegularExpressionSearch)
{
    SQLDateFormatter.format = "yyyy-MM-dd'T'HH:mm:ss.SSS"
}

      

+2


source







All Articles