Golang program to display time in different country’s format


In Golang we can use the functions like time.Format() and Now() Time to display the time in different country’s format. Now function from the time package is used to get the current local time and the format function is used to format the time string using different format strings.

Syntax

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.

func Now() Time

The Now() function is defined in time package. This function generates the current local time. To use this function, we have to first import the time package in our program.

Algorithm

  • Step 1 − Import the required packages in the program

  • Step 2 − Create a main function

  • Step 3 − In the main, use built-in function to display time in different country’s format

  • Step 4 − Print the Output using Println function where ln means new line

Example 1

In this example, we will write a Golang program to display time in different country’s format using Format method. Different formats will be passed in the function for different countries.

package main

import (
   "fmt"
   "time"
)

func main() {
   currentTime := time.Now()
   fmt.Println("Time in US format:", currentTime.Format("March 02, 2006 06:04:06 PM MST"))	
   fmt.Println("Time in UK format:", currentTime.Format("02 March 2006 15:04:06"))	
   fmt.Println("Time in France format:", currentTime.Format("02/03/2006 15:04:06"))	
   fmt.Println("Time in Germany format:", currentTime.Format("02.03.2006 15:04:06"))
}

Output

Time in US format: March 03, 2023 23:28:23 AM UTC
Time in UK format: 03 March 2023 04:28:23
Time in France format: 03/04/2023 04:28:23
Time in Germany format: 03.04.2023 04:28:23

Example 2

In this illustration, we will write a Go program to display time in different country’s format using shorthand variable. The time will be stored in a shorthand variable and the variable will be printed

package main

import (
   "fmt"
   "time"
)

func main() {
   currentTime := time.Now()

   usTime := currentTime.Format("March 02, 2006 06:04:05 PM MST")
   fmt.Println("Time in US format:", usTime)

   ukTime := currentTime.Format("02 March 2006 15:04:06")
   fmt.Println("Time in UK format:", ukTime)
	
   franceTime := currentTime.Format("02/03/2006 15:04:06")
   fmt.Println("Time in France format:", franceTime)

   germanyTime := currentTime.Format("02.03.2006 15:04:05")
   fmt.Println("Time in Germany format:", germanyTime)
}

Output

Time in US format: March 03, 2023 23:29:17 AM UTC
Time in UK format: 03 March 2023 04:29:23
Time in France format: 03/04/2023 04:29:23
Time in Germany format: 03.04.2023 04:29:17

Conclusion

We executed and compiled the program of displaying time in different country’s format using two examples. Both the examples are executed in a similar way but in the second example the time format string is stored in the shorthand variable. Both the examples give desired Output.

Updated on: 03-Apr-2023

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements