Parsing Dates from a String and Formatting
·
248 words
·
2 minutes read
We almost touched on this in our post about unix time - but in this post we look at how to take an arbitrary date in string format and convert it into a meaningful date in the format we want. This uses two important functions, Parse
and Format
within the time package.
The parse function is interesting because, unlike some programming languages, to parse a date you don’t specify the date using letters (Y-m-d for example). Instead you use a real time as the format - this time in fact: 2006-01-02T15:04:05+07:00
.
|
|
Some existing date constants include:
Name (Constant) | Format |
---|---|
RFC822 | 02 Jan 06 15:04 MST |
RFC850 | Monday, 02-Jan-06 15:04:05 MST |
RFC1123 | Mon, 02 Jan 2006 15:04:05 MST |
RFC3339 | 2006-01-02T15:04:05Z07:00 |
RFC3339Nano | 2006-01-02T15:04:05.999999999Z07:00 |
To use the MySQL date format, you’d use: “2006-01-02 15:04:05”