How to Print Specific date-time in Golang?
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:
package main
import "fmt"
import "time"
func main() {
dt := time .Now()
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:
package main
import "fmt"
import "time"
func main() {
dt := time .Now()
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:
package main
import "fmt"
import "time"
func main() {
dt := time .Now()
fmt.Println(dt.Format( "01-02-2006" ))
fmt.Println(dt.Format( "01-02-2006 15:04:05" ))
fmt.Println(dt.Format( "01-02-2006 15:04:05 Mon" ))
fmt.Println(dt.Format( "01-02-2006 15:04:05 Monday" ))
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
Last Updated :
17 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...