Last active
June 30, 2022 07:12
-
-
Save iyashjayesh/e0d1d73ebf422d1b9b8df3f610aa468c to your computer and use it in GitHub Desktop.
How to use Time package to get Time & date in various formats.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can edit this code! | |
package main | |
import ( | |
"log" | |
"time" | |
) | |
func main() { | |
// get current time in UTC timezone | |
currentTime := time.Now() | |
log.Println("CurrentTime: ", currentTime) | |
// To get the custom time in UTC timezone, we can use the following function: | |
t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) // year, month, day, hour, minute, second, nanosecond, location | |
log.Println("Time: ", t) | |
log.Println("Time in UTC: ", t.UTC()) | |
log.Println("Time in Local: ", t.Local()) | |
log.Println("Time in Location: ", t.In(time.FixedZone("UTC", 0))) | |
log.Println("Get the time - weekday: ", t.Weekday()) | |
log.Println("Get the time - year: ", t.Year()) | |
log.Println("Get the time - month: ", t.Month()) | |
log.Println("Get the time - day: ", t.Day()) | |
log.Println("Get the time - hour: ", t.Hour()) | |
log.Println("Get the time - minute: ", t.Minute()) | |
log.Println("Get the time - second: ", t.Second()) | |
log.Println("Get the time - nanosecond: ", t.Nanosecond()) | |
log.Println("Get the time - location: ", t.Location()) | |
// get current time in seconds since Unix epoch (January 1, 1970 UTC) | |
secs := time.Now().Unix() | |
log.Println("Secs: ", secs) | |
// get current time in milliseconds since Unix epoch (January 1, 1970 UTC) | |
milliSecs := time.Now().UnixNano() / 1000000 | |
log.Println("MilliSecs: ", milliSecs) | |
// or | |
// get current time in milliseconds since Unix epoch (January 1, 1970 UTC) | |
milli := time.Now().UnixMilli() | |
log.Println("Milli: ", milli) | |
// get current time in microseconds since Unix epoch (January 1, 1970 UTC) | |
microSecs := time.Now().UnixNano() / 1000 | |
log.Println("MicroSecs: ", microSecs) | |
// get current time in nanoseconds since Unix epoch (January 1, 1970 UTC) | |
nanoSecs := time.Now().UnixNano() | |
log.Println("NanoSecs: ", nanoSecs) | |
// get current time in available formats | |
// get current time in RFC 3339 format | |
isoTime := time.Now().Format(time.RFC3339) | |
log.Println("ISO Time: ", isoTime) | |
// get current time in RFC 1123 format | |
rfcTime := time.Now().Format(time.RFC1123) | |
log.Println("RFC Time: ", rfcTime) | |
// print all the time formats in the local timezone | |
log.Println("Time in Local: ", time.Now().Format(time.Kitchen)) | |
log.Println("Time in Local: ", time.Now().Format(time.Stamp)) | |
log.Println("Time in Local: ", time.Now().Format(time.StampMilli)) | |
log.Println("Time in Local: ", time.Now().Format(time.StampMicro)) | |
log.Println("Time in Local: ", time.Now().Format(time.StampNano)) | |
log.Println("Time in Local: ", time.Now().Format(time.RFC3339)) | |
log.Println("Time in Local: ", time.Now().Format(time.RFC1123)) | |
// To format a time in a specific format, we can use the bellow code: | |
// format a time in string in the given layout | |
t = time.Now() | |
log.Println(t.Format("2006-01-02 15:04:05")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment