Skip to content

Instantly share code, notes, and snippets.

@gimsesu
Last active October 14, 2020 06:07
Show Gist options
  • Save gimsesu/b6f350d716e93592116b5c0a3be92c4c to your computer and use it in GitHub Desktop.
Save gimsesu/b6f350d716e93592116b5c0a3be92c4c to your computer and use it in GitHub Desktop.
Snippet: Subtract month (golang)
package main
import (
"fmt"
"time"
)
func subtractMonth(startT time.Time, idx int) (result time.Time, err error) {
loc, err := time.LoadLocation("Asia/Seoul")
if err != nil {
log.Println(err)
return
}
y, m, _ := startT.Date()
return time.Date(y, m-time.Month(idx), 1, 0, 0, 0, 0, loc), nil
}
func main() {
start := "2020-06-30 05:10:59"
startT, err := time.Parse(global.LAYOUTYMDT, start)
if err != nil {
log.Println(err)
}
result := subtractMonth(start, 1)
fmt.Println(result)
// 2020-05-01 00:00:00 +0900 KST
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment