How to Print Specific date-time in Golang?
Last Updated :
17 May, 2020
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:
C
// 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:
C
// 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:
C
// 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
Similar Reads
How to Get the String in Specified Base in Golang? Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a FormatInt() function which is used to return the string representation of x in the given base, i.e., 2 <= base <= 36. Here, the resul
2 min read
How to compare times in Golang? With the help of Before() and After() and Equal(), function we can compare the time as well as date but we are also going to use the time.Now() and time.Now().Add() function for comparison. Functions Used: These functions compares the times as seconds. Before(temp) - This function is used to check i
2 min read
How to Trim a String in Golang? In Go, strings are UTF-8 encoded sequences of variable-width characters, unlike some other languages like Java, python and C++. Go provides several functions within the strings package to trim characters from strings.In this article we will learn how to Trim a String in Golang.Examples := "@@Hello,
2 min read
How to Get Current time in Golang? With the help of time.Now() function, we can get the current time in Golang by importing time module. Syntax: time.Now() Return: Return current date and time. Example #1: In this example, we can see that by using time.Now() function, we are able to get the current date and time. C // Golang program
1 min read
How to Format date using strftime() in Python ? In this article, we will see how to format date using strftime() in Python. localtime() and gmtime() returns a tuple representing a time and this tuple is converted into a string as specified by the format argument using python time method strftime(). Syntax: time.strftime(format[, sec]) sec: This i
2 min read