Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Date Arithmetic

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now()
	fmt.Printf("%T\n", t)
	fmt.Printf("Now:              %v\n", t)

	d2 := t.AddDate(0, 0, 2)
	fmt.Printf("2 days from now:  %v\n", d2)

	y1 := t.AddDate(1, 0, 0)
	fmt.Printf("A year from now:  %v\n", y1)

	m1 := t.AddDate(0, -1, 0)
	fmt.Printf("Last month:       %v\n", m1)

	fmt.Println()
	fmt.Printf("Now:              %v\n", t)

}
time.Time
Now:              2020-04-24 22:20:50.306276218 +0300 IDT m=+0.000028191
2 days from now:  2020-04-26 22:20:50.306276218 +0300 IDT
A year from now:  2021-04-24 22:20:50.306276218 +0300 IDT
Last month:       2020-03-24 22:20:50.306276218 +0200 IST

Now:              2020-04-24 22:20:50.306276218 +0300 IDT m=+0.000028191