Q:

Golang program to print date and time according to the specified location

belongs to collection: Golang Date & Time Programs

0

In this program, we will get the date and time of the specified location using the LoadLocation() and In() function and print the result on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to print the date and time according to the specified location is given below. The given program is compiled and executed successfully.

// Golang program to print date and time
// according to the specified location

package main

import "fmt"
import "time"

func main() {
	loc, error := time.LoadLocation("America/Los_Angeles")
	if error != nil {
		fmt.Println("Unable to load location")
	}

	ttime := time.Now()
	fmt.Println(ttime.In(loc))
}

Output:

2021-03-22 22:54:35.145256942 -0700 PDT

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt and time packages to use time and fmt related functions.

In the main() function, we loaded location "America/Los_Angeles" using LoadLocation() function. Then get the current date and time using the Now() function. After that, we printed the current date-time of "Los Angeles" using In() function on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Date & Time Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang program to print all months using predefine... >>
<< Golang program to demonstrate the Seconds() functi...