Calculate Dates in Golang

Create new folder named src. In src folder, create new file named main.go as below:

package main

import (
	"fmt"
	"time"
)

func main() {
	Demo1()
	Demo2()
	Demo3()
}

func Demo1() {
	today := time.Now()

	fmt.Println("Add Days")
	time1 := today.AddDate(0, 0, 2)
	fmt.Println("time 1:", time1.Format("02/01/2006"))

	fmt.Println("Add Months")
	time2 := today.AddDate(0, 3, 0)
	fmt.Println("time 2:", time2.Format("02/01/2006"))

	fmt.Println("Add Years")
	time3 := today.AddDate(4, 0, 0)
	fmt.Println("time 3:", time3.Format("02/01/2006"))

	time4 := today.Add(2 * 24 * time.Hour)
	fmt.Println("time 4:", time4.Format("02/01/2006"))
}

func Demo2() {
	strDate1 := "20/10/2018"
	date1, _ := time.Parse("02/01/2006", strDate1)
	strDate2 := "25/10/2018"
	date2, _ := time.Parse("02/01/2006", strDate2)
	result1 := date2.Sub(date1).Hours()
	fmt.Println("Hours: ", result1)
	result2 := date2.Sub(date1).Minutes()
	fmt.Println("Minutes: ", result2)
}

func Demo3() {
	date1 := time.Date(2018, 11, 20, 0, 0, 0, 0, time.UTC)
	date2 := time.Date(2018, 11, 25, 0, 0, 0, 0, time.UTC)

	result1 := date1.Before(date2)
	fmt.Println("Before: ", result1)

	result2 := date1.After(date2)
	fmt.Println("After: ", result2)

	result3 := date1.Equal(date2)
	fmt.Println("Equal: ", result3)
}




Open Terminal windows in Visual Studio Code and run command line: go run main.go

Add Days
time 1: 02/04/2019
Add Months
time 2: 01/07/2019
Add Years
time 3: 31/03/2023
time 4: 02/04/2019

Hours: 120
Minutes: 7200

Before: true
After: false
Equal: false