Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.
In below example AddDate and Add function used from Golang Time package.
Syntax
func (t Time) AddDate(years int, months int, days int) Time
func (t Time) Add(d Duration) Time
Example
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Today:", now)
after := now.AddDate(-1, 0, 0)
fmt.Println("Subtract 1 Year:", after)
after = now.AddDate(0, -1, 0)
fmt.Println("Subtract 1 Month:", after)
after = now.AddDate(0, 0, -1)
fmt.Println("Subtract 1 Day:", after)
after = now.AddDate(-2, -2, -5)
fmt.Println("Subtract multiple values:", after)
after = now.Add(-10*time.Minute)
fmt.Println("Subtract 10 Minutes:", after)
after = now.Add(-10*time.Second)
fmt.Println("Subtract 10 Second:", after)
after = now.Add(-10*time.Hour)
fmt.Println("Subtract 10 Hour:", after)
after = now.Add(-10*time.Millisecond)
fmt.Println("Subtract 10 Millisecond:", after)
after = now.Add(-10*time.Microsecond)
fmt.Println("Subtract 10 Microsecond:", after)
after = now.Add(-10*time.Nanosecond)
fmt.Println("Subtract 10 Nanosecond:", after)
}
Output
Today: 2017-08-27 12:21:17.8379942 +0530 IST
Subtract 1 Year: 2016-08-27 12:21:17.8379942 +0530 IST
Subtract 1 Month: 2017-07-27 12:21:17.8379942 +0530 IST
Subtract 1 Day: 2017-08-26 12:21:17.8379942 +0530 IST
Subtract multiple values: 2015-06-22 12:21:17.8379942 +0530 IST
Subtract 10 Minutes: 2017-08-27 12:11:17.8379942 +0530 IST
Subtract 10 Second: 2017-08-27 12:21:07.8379942 +0530 IST
Subtract 10 Hour: 2017-08-27 02:21:17.8379942 +0530 IST
Subtract 10 Millisecond: 2017-08-27 12:21:17.8279942 +0530 IST
Subtract 10 Microsecond: 2017-08-27 12:21:17.8379842 +0530 IST
Subtract 10 Nanosecond: 2017-08-27 12:21:17.83799419 +0530 IST
Most Helpful This Week
Golang import package inside package
How to get current IP form ipify.org ?
Constructors in Golang
How to verify a string only contains letters, numbers, underscores, and dashes in Golang?
Various examples of printing and formatting in Golang
How to read input from console line?
Example to create custom error
How to fix race condition using Atomic Functions in Golang?
How to wait for Goroutines to Finish Execution?
How to print string with double quote in Go?