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.
Examples:
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
Code:
package main
import (
"fmt"
"time"
)
func main() {
myDateString := "2018-01-20 04:35"
fmt.Println( "My Starting Date:\t" , myDateString)
myDate, err := time .Parse( "2006-01-02 15:04" , myDateString)
if err != nil {
panic(err)
}
fmt.Println( "My Date Reformatted:\t" , myDate.Format( time .RFC822))
fmt.Println( "Just The Date:\t\t" , myDate.Format( "2006-01-02" ))
}
|
Output:
My Starting Date: 2018-01-20 04:35
My Date Reformatted: 20 Jan 18 04:35 UTC
Just The Date: 2018-01-20
Last Updated :
08 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...