Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Parsing Time in Golang

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Parsing time is to convert our time to Golang time object so that we can extract information such as date, month, etc from it easily. We can parse any time by using time.Parse function which takes our time string and format in which our string is written as input and if there is no error in our format, it will return a Golang time object.


Input : "2018-04-24"
Output : 2018-04-24 00:00:00 +0000 UTC

Input : "04/08/2017"
Output : 2017-04-08 00:00:00 +0000 UTC


// Golang program to show the parsing of time
package main
import (
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 {
    // 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 YY-MM-DD
    fmt.Println("Just The Date:\t\t", myDate.Format("2006-01-02"))


My Starting Date:     2018-01-20 04:35
My Date Reformatted:     20 Jan 18 04:35 UTC
Just The Date:         2018-01-20
My Personal Notes arrow_drop_up
Last Updated : 08 Jun, 2020
Like Article
Save Article
Similar Reads