Golang program to parse time


In golang we can use various internal time function to parse time. Time is parsed using parse and ParseInLocation method.

Syntax

time.Parse()

This function belongs to time package. It is used to parse the string into time.Time value and it takes two inputs: the layout and the time string which is to be parsed.

time.ParselnLocation()

This function is a part of the time package. It is used to parse the string into time.Time value along with a specified location. It takes 3 arguments: the location, layout string and the time string.

time.Format()

This function is present in the time package. It takes a layout input string that depicts how the Output string will be formatted.

time.LoadLocation()

This function belongs to the time package and it is used to load the location by returning *time.Location.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and time package in the program where main produces executable codes and fmt helps in formatting input and Output

  • Step 2 − Create a main function and using the const keyword mention the format in which in which the time will be parsed

  • Step 3 − In this step, mention the time string which is to be parsed and assign it to time-str

  • Step 4 − Then, use Parse function from the time package to parse the time with two inputs the layout and the time_str which is to be parsed

  • Step 5 − If any error is encountered while parsing the time error will be printed on the console and return

  • Step 6 − Print the parsed time on the console using different format by applying Format function

  • Step 7 − The print statement is executed using Println function from the fmt package where ln means new line

Example 1

In this example, we will write a Golang program to parse time using Parse method with two inputs the layout defines the format in which the parsed time will be printed, next is the time string which is to be parsed.

package main

import (
   "fmt"
   "time"
)

func main() {	
   const layout = "2006-01-02 15:04:05"	
   time_str := "2023-03-06 08:30:00"	
   t, err := time.Parse(layout, time_str)
   if err != nil {
      fmt.Println("Error parsing time:", err)
      return
   }	
   fmt.Println("Parsed time:", t.Format("Mon Jan 2 15:04:05 2006"))
}

Output

Parsed time: Mon Mar 6 08:30:00 2023

Example 2

In this example, we will write a Golang program to parse time using ParselnLocation method in which LoadLocation method is used to get the time zone to parse the time and the location obtained will be passed in the method.

package main

import (
   "fmt"
   "time"
)

func main() {	
   const layout = "2006-01-02 15:04:05"	
   time_str := "2023-03-06 08:30:00"	
   loc, err := time.LoadLocation("America/New_York")
   if err != nil {
      fmt.Println("Error loading location:", err)
      return
   }	
   t, err := time.ParseInLocation(layout, time_str, loc)
   if err != nil {
      fmt.Println("Error parsing time:", err)
      return
   }	
   fmt.Println("Parsed time:", t.Format("Mon Jan 2 15:04:05 2006 MST"))
}

Output

Parsed time: Mon Mar 6 08:30:00 2023 EST

Conclusion

We executed and compiled the program of parsing time using two examples. In the first example, Parse method is used to parse the time and in the second example, time zone is used in the ParseInLocation to parse the time.

Updated on: 04-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements