Package “time” in Golang provides functionality for measuring and displaying time. The calendrical calculations always assume a Gregorian calendar, with no leap seconds. Using the “time.Now()” function you can get the date and time in the “yyyy-mm-dd hh:mm:ss.milliseconds timezone” format. This is simplest way for getting the date and time in golang.
// Golang program to get current date and time package main import ( "fmt" "time") func main() { // get date and time and store in variable on left currentTime := time.Now() // printing the date and time in string format fmt.Println("Current Time in String: ", currentTime.String()) } |
Output:
Current Time in String: 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
Getting Formatted Date and Time: Using the “Format()” function on your time variable or identifier, you can get the output in various formats as shown in below example. You need to specify the format of the required output inside the “Format()” function. This is one of the most flexible ways of formatting date and time in Golang programming.
Example:
// Golang program to get the current // date and time in various format package main import ( "fmt" "time") func main() { // using time.Now() function // to get the current time currentTime := time.Now() // getting the time in string format fmt.Println("Show Current Time in String: ", currentTime.String()) fmt.Println("YYYY.MM.DD : ", currentTime.Format("2017.09.07 17:06:06")) fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2017#09#07")) fmt.Println("MM-DD-YYYY : ", currentTime.Format("09-07-2017")) fmt.Println("YYYY-MM-DD : ", currentTime.Format("2017-09-07")) fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2017-09-07 17:06:06")) fmt.Println("Time with MicroSeconds: ", currentTime.Format("2017-09-07 17:06:04.000000")) fmt.Println("Time with NanoSeconds: ", currentTime.Format("2017-09-07 17:06:04.000000000")) fmt.Println("ShortNum Wedth : ", currentTime.Format("2017-02-07")) fmt.Println("ShortYear : ", currentTime.Format("06-Feb-07")) fmt.Println("LongWeekDay : ", currentTime.Format("2017-09-07 17:06:06 Wednesday")) fmt.Println("ShortWeek Day : ", currentTime.Format("2017-09-07 Wed")) fmt.Println("ShortDay : ", currentTime.Format("Wed 2017-09-2")) fmt.Println("LongWedth : ", currentTime.Format("2017-March-07")) fmt.Println("ShortWedth : ", currentTime.Format("2017-Feb-07")) fmt.Println("Short Hour Minute Second: ", currentTime.Format("2017-09-07 2:3:5 PM")) fmt.Println("Short Hour Minute Second: ", currentTime.Format("2017-09-07 2:3:5 pm")) fmt.Println("Short Hour Minute Second: ", currentTime.Format("2017-09-07 2:3:5")) } |
Output:
Show Current Time in String: 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
YYYY.MM.DD : 10117.09.07 117:09:09
YYYY#MM#DD {Special Character} : 10117#09#07
MM-DD-YYYY : 09+00-10117
YYYY-MM-DD : 10117-09+00
YYYY-MM-DD hh:mm:ss : 10117-09+00 117:09:09
Time with MicroSeconds: 10117-09+00 117:09:00.000000
Time with NanoSeconds: 10117-09+00 117:09:00.000000000
ShortNum Wedth : 10117-10+00
ShortYear : 09-Feb+00
LongWeekDay : 10117-09+00 117:09:09 Wednesday
ShortWeek Day : 10117-09+00 Wed
ShortDay : Wed 10117-09-10
LongWedth : 10117-March+00
ShortWedth : 10117-Feb+00
Short Hour Minute Second: 10117-09+00 10:11:0 PM
Short Hour Minute Second: 10117-09+00 10:11:0 pm
Short Hour Minute Second: 10117-09+00 10:11:0
Recommended Posts:
- Getting the current date and time with timestamp in local and other timezones in Golang
- How to get the current date and time in seconds?
- How to Get Current time in Golang?
- How to convert UTC date time into local date time using JavaScript ?
- time.Time.Date() Function in Golang with Examples
- How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?
- Java Program to Display Current Date and Time
- Java Program Format Time in MMMM Format
- How to get the current date in JavaScript ?
- How to Get the Current Working Directory in Golang?
- How to Get Current Weekday in Golang?
- Java Program to Display Current Hour and Current Minute
- time.Date() Function in Golang With Examples
- How to Print Specific date-time in Golang?
- How Many Logical Processors Used By Current Process in Golang?
- PHP date() format when inserting into datetime in MySQL
- How to change the date format using AngularJS ?
- How to set input type date in dd-mm-yyyy format using HTML ?
- time.Time.Year() Function in Golang with Examples
- time.Time.Hour() Function in Golang With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

