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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
	"fmt"
	"time"
)

func main() {

	// The date we're trying to parse, work with and format
	myDateString := "2018-01-20 04:35"
	fmt.Println("My Starting Date:\t", myDateString)

	// Parse the date string into Go's time object
	// The 1st param specifies the format, 2nd is our date string
	myDate, err := time.Parse("2006-01-02 15:04", myDateString)
	if err != nil {
		panic(err)
	}

	// Format uses the same formatting style as parse, or we can use a pre-made constant
	fmt.Println("My Date Reformatted:\t", myDate.Format(time.RFC822))

	// In Y-m-d
	fmt.Println("Just The Date:\t\t", myDate.Format("2006-01-02"))
}

parsing dates example

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”

Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!