如何在Golang中找到上个月和上一年的日期。

huangapple go评论80阅读模式
英文:

How to find previous month and year in golang

问题

我刚刚发现AddDate()函数并不总是按预期工作。

例如:

mayEndDate := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
finalDate := endOfMay.AddDate(0, -1, 0)

在这里
输出:

  • myEndDate = 2021-05-31 12:00:00 +0000 UTC
  • finalDate = 2021-05-01 12:00:00 +0000 UTC

我本来期望finalDate是四月的日期。
阅读文档后,我找到了原因。

> AddDate函数的结果与Date函数的结果一样进行了规范化,所以例如将十月三十一日加上一个月会得到十二月一日,这是十一月三十一日的规范化形式。

***我的问题:***如何正确地从今天的日期中找到上个月的日期?

英文:

I just found AddDate() does not always works as expected.

ex:

mayEndDate := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
finalDate := endOfMay.AddDate(0, -1, 0)

here
output:

  • myEndDate = 2021-05-31 12:00:00 +0000 UTC
  • finalDate = 2021-05-01 12:00:00 +0000 UTC

I was expecting finalDate to be in April.
After reading the documentation, I found out the reason.

> AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

My question: how to now correctly find out the last month's date from today's date?

答案1

得分: 3

获取当前月份使用Month(),然后从那里获取上一个月份就很简单了:

currentMonth := mayEndDate.Month()
previousMonth := currentMonth - 1
if currentMonth == time.January {
    previousMonth = time.December
}
英文:

Get the current month using Month(), then from there it’s pretty simple to get the previous one:

currentMonth := mayEndDate.Month()
previousMonth := currentMonth - 1
if currentMonth == time.January {
    previousMonth = time.December
}

答案2

得分: 3

package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}
英文:

> if date is 2021-05-31 then previous month is April 2021.


package main

import (
	"fmt"
	"time"
)

func prevMonth(t time.Time) (int, time.Month) {
	y, m, _ := t.Date()
	y, m, _ = time.Date(y, m-1, 1, 0, 0, 0, 0, time.UTC).Date()
	return y, m
}

func main() {
	endOfMay := time.Date(2021, 5, 31, 12, 00, 00, 00, time.UTC)
	fmt.Println(endOfMay)
	fmt.Println(prevMonth(endOfMay))
}

https://go.dev/play/p/rP25ramRrZ3

2021-05-31 12:00:00 +0000 UTC
2021 April

huangapple
  • 本文由 发表于 2022年12月2日 01:12:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/74645681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定