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

No example for Dates in ReadMe #49

Open
meurig opened this issue Nov 18, 2018 · 1 comment
Open

No example for Dates in ReadMe #49

meurig opened this issue Nov 18, 2018 · 1 comment

Comments

@meurig
Copy link

meurig commented Nov 18, 2018

Hi, and thanks for the great library!

I'm pretty new to all this and trying to map a json result to a Date. I notice you've merged some code adding support for Dates (that's great!) but it would people like me if the documentation gave a brief example for how this works.

Many thanks,

Meurig

@meurig
Copy link
Author

meurig commented Nov 18, 2018

I've no idea if this is the best way to do it, but for reference I've ended up with:

timestamp = try object.value(for: .timestamp, with: DateFormatTransform())

Where DateFormatTransform is declared as:

public class DateFormatTransform: DateTransformer {
    public func date(from string: String) throws -> Date {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
        return dateformatter.date(from: string)!
    }
    
    public func string(form date: Date) -> String {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
        return dateformatter.string(from: date)
    }
}

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

1 participant