Posts

Showing posts with the label Date

How to add/subtract Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to a date in Go Language

Image
Hello everyone, In this tutorial, we will learn h ow to add/subtract Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to date in Go Language. The time package in Go language provides Add() function which  is used to add/subtract a duration to time t. Since duration can be represented in hours, minutes, seconds, milliseconds, microseconds and nanoseconds, The signature of the Add method is: func (t Time) Add(d Duration) Time Example 1: Go Program to add/subtract minutes. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now() fmt.Println( "Current date and time : " , dt1) //Adding 30 minutes dt2 := dt1.Add( 30 * time.Minute) fmt.Println( "After 30 minutes:" , dt2) //Subtract 3 minutes dt3 := dt1.Add(- 3 * time.Minute) fmt.Println( "Before 3 minutes:" , dt3) } Output: Example 2: Go Program to add/subtract seconds. package main import (

How to add/subtract days, months or years to a date in Go Language

Image
Hello everyone, In this tutorial, we will learn h ow to add/subtract days, months or years to a date in Go Language. The time package in Go language provides AddDate() function which is used to add/subtract years, months and days to time t.  The signature of the AddDate method is: func (t Time) AddDate(years int , months int , days int ) Time Example 1: Go Program to add/subtract days. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now() fmt.Println( "Current date and time : " , dt1) //Adding 3 days dt2 := dt1.AddDate( 0 , 0 , 3 ) fmt.Println( "After three days: " , dt2) //Subtract 400 days dt3 := dt1.AddDate( 0 , 0 , - 400 ) fmt.Println( "Before 400 days: " , dt3) } Output: Example 2: Go Program to add/subtract months. package main import ( "fmt" "time" ) func main() { //Current date and time dt1 := time.Now()

Go Language - Get Current Date and Time

Image
Hello everyone, today we are going to learn how to get the current date and time in Go Language. We need to import the “time” package in our Go script to work with date and time. Go Program to find current Date and Time: package main import ( "fmt" "time" ) func main() { dt := time.Now() fmt.Println(dt.String()) //Formatted Date and Time //Format MM/DD/YYYY fmt.Println(dt.Format( "01/02/2006" )) //Format MM-DD-YYYY fmt.Println(dt.Format( "01-02-2006" )) //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 ShortWeek Day fmt.Println(dt.Format( "01-02-2006 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" )) //Include n