Golang supports time formatting and parsing via pattern-based layouts. In Go, the current time can be determined by using time.Now(), provided by the time package. Package time provides functionality for measuring and displaying the time.
To print Current date-time you need to import the “time” package in your Go program to work with date and time.
Example:
// Golang program to show // the use of time.Now() Function package main import "fmt"import "time" func main() { dt := time.Now() // printing the time in string format fmt.Println("Current date and time is: ", dt.String()) } |
Output :
Current date and time is: 2020-05-05 06:43:01.419199824 +0000 UTC m=+0.000076701
To print Specific date-time in Golang use format constants provided in the time package. The commonly used formats are:
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
Example:
// Golang program to print specific date and time package main import "fmt"import "time" func main() { dt := time.Now() // printing date and time in UnixDate format fmt.Println("Specific date and time is: ", dt.Format(time.UnixDate)) } |
Output:
Specific date and time is: Tue May 5 07:05:00 UTC 2020
Example:
// Golang program to print specific date and time package main import "fmt"import "time" func main() { dt := time.Now() // Format MM-DD-YYYY fmt.Println(dt.Format("01-02-2006")) // Format MM-DD-YYYY hh:mm:ss fmt.Println(dt.Format("01-02-2006 15:04:05")) // With short weekday (Mon) fmt.Println(dt.Format("01-02-2006 15:04:05 Mon")) // With weekday (Monday) fmt.Println(dt.Format("01-02-2006 15:04:05 Monday")) // Include micro seconds fmt.Println(dt.Format("01-02-2006 15:04:05.000000")) } |
Output:
11-10-2009 11-10-2009 23:00:00 11-10-2009 23:00:00 Tue 11-10-2009 23:00:00 Tuesday 11-10-2009 23:00:00.000000
Recommended Posts:
- Repeating a String for Specific Number of Times in Golang
- How to count specific characters present in the slice in Golang?
- PHP | Converting string to Date and DateTime
- PHP date() format when inserting into datetime in MySQL
- HTML | DOM Input Datetime Object
- How do you display JavaScript datetime in 12 hour AM/PM format?
- How to copy a DateTime object in PHP ?
- How to print string with double quotes in Golang?
- How to Create and Print Multi Dimensional Slice in Golang?
- fmt.print() Function in Golang With Examples
- How to print struct variables data in Golang?
- Domain Specific Tools
- Removing occurrences of a specific character from end of a string in PHP
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- How to get specific number of child elements using CSS?
- How to insert an item into array at specific index in JavaScript?
- How to adjust CSS for specific zoom level?
- How to determine if an array contains a specific value in PHP?
- How to insert a string at a specific index in JavaScript ?
- How to reload page after specific seconds in jQuery ?
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.

