- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Getting the current date and time with timestamp in local and other timezones in Golang
In Go, it's easy to work with dates and times using the time package. This package provides a set of functions and types for working with dates and times, including the ability to get the current date and time with a timestamp in local and other timezones.
In this article, we'll explore how to get the current date and time with a timestamp in local and other timezones using Go.
Getting the current date and time with timestamp in local timezone
To get the current date and time with a timestamp in the local timezone, we can use the time.Now() function. This function returns a time.Time value that represents the current date and time in the local timezone.
Here's an example of how to use time.Now() to get the current date and time in the local timezone −
Example
package main import ( "fmt" "time" ) func main() { currentTime := time.Now() fmt.Println("Current time in local timezone:", currentTime) }
Output
Current time in local timezone: 2023-04-16 06:59:33.794750864 +0000 UTC m=+0.000014548
This will output the current date and time in the local timezone in the format YYYY-MM-DD HH:MM:SS +0000 UTC.
Getting the current date and time with timestamp in other timezones
To get the current date and time with a timestamp in a specific timezone, we can use the time.LoadLocation() function to load the timezone, and then use the time.Now().In() function to convert the current time to that timezone.
Here's an example of how to use time.LoadLocation() and time.Now().In() to get the current date and time with a timestamp in the America/New_York timezone −
Example
package main import ( "fmt" "time" ) func main() { location, err := time.LoadLocation("America/New_York") if err != nil { fmt.Println("Error loading location:", err) return } currentTime := time.Now().In(location) fmt.Println("Current time in New York timezone:", currentTime) }
This will output the current date and time in the America/New_York timezone in the format YYYY-MM-DD HH:MM:SS -0500 EST.
Conclusion
In this article, we've explored how to get the current date and time with a timestamp in local and other timezones using Go. We used the time package to get the current date and time in the local timezone, and to load and convert the current time to other timezones.
Working with dates and times is a common task in many applications, and Go's time package provides a robust set of functions and types to make this task easy and intuitive. With the examples in this article, you should now have a good foundation for working with dates and times in your own Go applications.