Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date and Time #421

Closed
rmoffett opened this issue Dec 10, 2015 · 11 comments
Closed

Date and Time #421

rmoffett opened this issue Dec 10, 2015 · 11 comments

Comments

@rmoffett
Copy link

Where is json["date"].date or json["time"].time?

@kukabi
Copy link

kukabi commented Dec 17, 2015

You may use an extension like below,

extension JSON {

    public var date: NSDate? {
        get {
            switch self.type {
            case .String:
                return Formatter.jsonDateFormatter.dateFromString(self.object as! String)
            default:
                return nil
            }
        }
    }

    public var dateTime: NSDate? {
        get {
            switch self.type {
            case .String:
                return Formatter.jsonDateTimeFormatter.dateFromString(self.object as! String)
            default:
                return nil
            }
        }
    }

}

and initialize formatters depending on the data you may receive.

class Formatter {

    private static var internalJsonDateFormatter: NSDateFormatter?
    private static var internalJsonDateTimeFormatter: NSDateFormatter?

    static var jsonDateFormatter: NSDateFormatter {
        if (internalJsonDateFormatter == nil) {
            internalJsonDateFormatter = NSDateFormatter()
            internalJsonDateFormatter!.dateFormat = "yyyy-MM-dd"
        }
        return internalJsonDateFormatter!
    }

    static var jsonDateTimeFormatter: NSDateFormatter {
        if (internalJsonDateTimeFormatter == nil) {
            internalJsonDateTimeFormatter = NSDateFormatter()
            internalJsonDateTimeFormatter!.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"
        }
        return internalJsonDateTimeFormatter!
    }

}

@craigpearce5
Copy link

I like this answer, however I have a question. If the dateFormat I enter in the Formatter class doesn't match the json, will it not return anything?

For example, my json date is 2016-01-27T12:01:40.915-07:00 and my dateFormat is "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'-'xxx" but nothing is returned. Is that because my dateFormat is wrong, or do I have issues elsewhere?

@lmacfadyen
Copy link

My JSON format is same as yours and here is the format I used: "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSZZ"

@craigpearce5
Copy link

@lmacfadyen Thanks very much, I will give that a try.

@craigpearce5
Copy link

@lmacfadyen That worked. Thanks very much.

@craigpearce5
Copy link

@rmoffett This issue should be closed.

@yesitsdave
Copy link

It would be good to add locale setting on the NSDateFormatter to this, otherwise it may fail depending on device locale.

@AlBlanc
Copy link

AlBlanc commented Jan 31, 2017

Thank you for this nice snippet, it saved me some time.

Here is the code updated for Swift3:

class Formatter {
    
    private static var internalJsonDateFormatter: DateFormatter?
    private static var internalJsonDateTimeFormatter: DateFormatter?
    
    static var jsonDateFormatter: DateFormatter {
        if (internalJsonDateFormatter == nil) {
            internalJsonDateFormatter = DateFormatter()
            internalJsonDateFormatter!.dateFormat = "yyyy-MM-dd"
        }
        return internalJsonDateFormatter!
    }
    
    static var jsonDateTimeFormatter: DateFormatter {
        if (internalJsonDateTimeFormatter == nil) {
            internalJsonDateTimeFormatter = DateFormatter()
            internalJsonDateTimeFormatter!.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"
        }
        return internalJsonDateTimeFormatter!
    }
    
}

extension JSON {
    
    public var date: Date? {
        get {
            switch self.type {
            case .string:
                return Formatter.jsonDateFormatter.date(from: self.object as! String)
            default:
                return nil
            }
        }
    }
    
    public var dateTime: Date? {
        get {
            switch self.type {
            case .string:
                return Formatter.jsonDateTimeFormatter.date(from: self.object as! String)
            default:
                return nil
            }
        }
    }
    
}

@OrkhanAlikhanov
Copy link

Formatter class can be reduced to:

class Formatter {
    static let jsonDateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    
    static let jsonDateTimeFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"
        return formatter
    }()
}

@lukemcgregor
Copy link

lukemcgregor commented Dec 14, 2017

In iOS 10+

    static let jsonDateTimeFormatter: DateFormatter = ISO8601DateFormatter()

@ykphuah
Copy link

ykphuah commented Mar 11, 2020

Is there a reason why this is not included? Will a PR be accepted?

I am using this with Outlook, and the dateTime doesn't send the milliseconds so the regex above will fail, I need to remove the SSS part and it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants