Golang : Get current, epoch time and display by year, month and day
Problem :
You need to get current and Epoch(Unix) time and display the time by year, month and day.
Solution :
Use the http://golang.org/pkg/time functions to get current and epoch time. There are methods to break the time down to year, month, day, hour and minutes as well.
For example :
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
epoch := now.Unix()
fmt.Println("Now: ", now)
fmt.Println("Epoch(Unix) Time: ", epoch)
fmt.Println(now.Format("Mon, Jan 2, 2006 at 3:04pm"))
fmt.Println("Day: ", now.Day())
fmt.Println("Month: ", now.Month())
fmt.Println("Year: ", now.Year())
fmt.Println("Hour: ", now.Hour())
fmt.Println("Minute: ", now.Minute())
}
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+2.9k Golang : PGX CopyFrom to insert rows into Postgres database
+35k Golang : Convert date or time stamp from string to time.Time type
+5.7k Mac/Linux/Windows : Get CPU information from command line
+8.4k Golang : Quadratic example
+4.4k Golang : Intercept, inject and replay HTTP traffics from web server
+9k Golang : Embed secret text string into binary(executable) file
+1.9k Golang : Fix go-cron set time not working issue
+6.1k Golang : Use modern ciphers only in secure connection
+9.3k Golang : ISO8601 Duration Parser example
+21.6k Golang : Strings to lowercase and uppercase example
+5.8k Golang : Calculate BMI and risk category
+14.1k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type