Last active
October 14, 2020 06:07
-
-
Save gimsesu/b6f350d716e93592116b5c0a3be92c4c to your computer and use it in GitHub Desktop.
Snippet: Subtract month (golang)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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