Skip to content

Instantly share code, notes, and snippets.

@Tee-Stark
Created September 1, 2022 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tee-Stark/828be7f11859ac0dafdfd9bff3ac6f59 to your computer and use it in GitHub Desktop.
Save Tee-Stark/828be7f11859ac0dafdfd9bff3ac6f59 to your computer and use it in GitHub Desktop.
Just a collection of some time and date manipulation functions in Go
package main
import (
"fmt"
"time"
)
func main() {
//// Get the current time.
myTime := time.Now() // get current time
fmt.Println("Current time in ", myTime.Location(), " is: ", myTime)
// Another way to get local time
location, _ := time.LoadLocation("Local") // LoadLocation("") also returns sets location to local
fmt.Println("Current time in ", location, " is: ", time.Now().In(location))
// change location to New York
location, _ = time.LoadLocation("America/New_York")
fmt.Println("Current time in ", location, " is: ", myTime.In(location))
// get time in Mountain Time Zone(MST)
location, _ = time.LoadLocation("MST")
fmt.Println("Current time in ", location, " is: ", myTime.In(location))
//// Using the Date() function.
year, month, day := myTime.Date()
fmt.Println("Year :", year)
fmt.Println("Month :", month)
fmt.Println("Day :", day)
//using the Clock() function
hour, min, sec := myTime.Clock()
fmt.Println("Hour :", hour)
fmt.Println("Minute :", min)
fmt.Println("Seconds :", sec)
//// using the function for each individual components of the time from Year to Nanosecond
fmt.Println("Year :", myTime.Year())
fmt.Println("Month :", myTime.Month())
fmt.Println("Day :", myTime.Day())
fmt.Println("Hour :", myTime.Hour())
fmt.Println("Minute :", myTime.Minute())
fmt.Println("Seconds :", myTime.Second())
fmt.Println("Nanosecond :", myTime.Nanosecond())
get individual components of the time from Year to Nanosecond from a specific date
yourTime := time.Date(2020, 07, 1, 06, 32, 10, 0, time.UTC)
fmt.Println("Year :", yourTime.Year())
fmt.Println("Month :", yourTime.Month())
fmt.Println("Day :", yourTime.Day())
fmt.Println("Hour :", yourTime.Hour())
fmt.Println("Minute :", yourTime.Minute())
fmt.Println("Seconds :", yourTime.Second())
fmt.Println("Nanosecond :", yourTime.Nanosecond())
// get time using Clock() function
yourHour, yourMin, yourSec := yourTime.Clock()
fmt.Println("Hour :", yourHour)
fmt.Println("Minute :", yourMin)
fmt.Println("Seconds :", yourSec)
// get time and date from string
dateString := "2021-09-05 07:35:20"
layout := "2006-01-02 15:04:05"
yourTime, _ = time.Parse(layout, dateString)
fmt.Println("Your time is: ", yourTime)
fmt.Println("Year :", yourTime.Year())
fmt.Println("Month :", yourTime.Month())
fmt.Println("Day :", yourTime.Day())
fmt.Println("Hour :", yourTime.Hour())
fmt.Println("Minute :", yourTime.Minute())
fmt.Println("Seconds :", yourTime.Second())
curTime := time.Now()
//add one hour to the time
curTime = curTime.Add(time.Hour)
fmt.Println("Current time is: ", curTime)
// Add one day to the time
tomorrow := curTime.Add(time.Hour * 24)
fmt.Println("This time tomorrow is: ", tomorrow)
// add one week to a time using Add()
nextWeek := curTime.Add(time.Hour * 24 * 7)
fmt.Println("This time next week is: ", nextWeek)
// add two days using the AddDate() function
nextTomorrow := curTime.AddDate(0, 0, 2)
fmt.Println("This time Next tomorrow is: ", nextTomorrow)
// add one month using AddDate()
nextMonth := curTime.AddDate(0, 1, 0)
fmt.Println("This time next month is: ", nextMonth)
// add five years using AddDate()
fiveYearsAndOneMonthAfter := curTime.AddDate(5, 1, 0)
fmt.Println("This time five years and one month after is: ", fiveYearsAndOneMonthAfter)
yesterday := curTime.AddDate(0, 0, -1)
fmt.Println("This time yesterday was: ", yesterday)
// subtract one month using Add()
lastMonth := curTime.Add(time.Hour * -24 * 30)
fmt.Println("This time last month was: ", lastMonth)
get difference between two dates
past := time.Date(2022, time.December, 25, 12, 0, 0, 0, time.UTC)
diff := past.Sub(curTime)
fmt.Println("Difference between now and the past is: ", diff)
// get the difference in various units
years := int(diff.Hours() / 24 / 365)
fmt.Println("Years: ", years)
months := int(diff.Hours() / 24 / 30)
fmt.Println("Months: ", months)
days := int(diff.Hours() / 24)
fmt.Println("Days: ", days)
hours := int(diff.Hours())
fmt.Println("Hours: ", hours)
minutes := int(diff.Minutes())
fmt.Println("Minutes: ", minutes)
seconds := int(diff.Seconds())
fmt.Println("Seconds: ", seconds)
milliseconds := int(diff.Milliseconds())
fmt.Println("Milliseconds: ", milliseconds)
// Get time and date in various formats
fmt.Println("Current time is: ", curTime)
// built-in standard formatting styles
fmt.Println("Current time is: ", curTime)
fmt.Println("Current time in RFC3339 format is: ", curTime.Format(time.RFC3339))
fmt.Println("Current time in RFC3339Nano format is: ", curTime.Format(time.RFC3339Nano))
fmt.Println("Current time in RFC1123 format is: ", curTime.Format(time.RFC1123))
fmt.Println("Current time in RFC1123Z format is: ", curTime.Format(time.RFC1123Z))
fmt.Println("Current time in RFC822 format is: ", curTime.Format(time.RFC822))
fmt.Println("Current time in RFC822Z format is: ", curTime.Format(time.RFC822Z))
fmt.Println("Current time in RFC850 format is: ", curTime.Format(time.RFC850))
fmt.Println("Current time in ANSIC format is: ", curTime.Format(time.ANSIC))
fmt.Println("Current time in Unix format is: ", curTime.Format(time.UnixDate))
// custom formatting styles
// DD-MM-YYYY HH:MM:SS
fmt.Println("Current time in custom format is: ", curTime.Format("02-01-2006 15:04:05"))
// MM-DD-YYYY HH:MM:SS
fmt.Println("Current time in custom format is: ", curTime.Format("01-02-2006 15:04:05"))
// YYYY-MM-DD HH:MM:SS
fmt.Println("Current time in custom format is: ", curTime.Format("2006-01-02 15:04:05"))
// DD.MM.YYYY
fmt.Println("Current time in custom format is: ", curTime.Format("02.01.2006"))
// DD/MM/YYYY
fmt.Println("Current time in custom format is: ", curTime.Format("02/01/2006"))
// 01 Feb 2006
fmt.Println("Current time in custom format is: ", curTime.Format("02 Jan 2006"))
// 01 February 2006 Monday
fmt.Println("Current time in custom format is: ", curTime.Format("02 February 2006 Monday"))
// 01 February 2006 Mon 15:04:05
fmt.Println("Current time in custom format is: ", curTime.Format("02 February 2006 Mon 15:04:05"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment