Skip to content

Instantly share code, notes, and snippets.

@spiegel-im-spiegel
Last active June 25, 2017 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spiegel-im-spiegel/bcf42df831165123cfc171fadf42eb1d to your computer and use it in GitHub Desktop.
Save spiegel-im-spiegel/bcf42df831165123cfc171fadf42eb1d to your computer and use it in GitHub Desktop.
Go 言語における無効日付(2月31日など)の扱い ref: http://qiita.com/spiegel-im-spiegel/items/42d115471cf2b5a80437
package main
import (
"fmt"
"time"
)
func main() {
tm := time.Date(2017, 1, 31, 0, 0, 0, 0, time.UTC)
for i := 0; i < 6; i++ {
fmt.Println(tm.Format("2006-01-02"))
tm = tm.AddDate(0, 1, 0)
}
}
2017-01-31
2017-03-03
2017-04-03
2017-05-03
2017-06-03
2017-07-03
package main
import (
"fmt"
"time"
)
func main() {
tm := time.Date(2017, 1, 31, 0, 0, 0, 0, time.UTC)
for i := 0; i < 6; i++ {
fmt.Println(tm.AddDate(0, i, 0).Format("2006-01-02"))
}
}
2017-01-31
2017-03-03
2017-03-31
2017-05-01
2017-05-31
2017-07-01
package main
import (
"fmt"
"time"
)
func main() {
tm := time.Date(2017, 1, 31, 0, 0, 0, 0, time.UTC)
for i := 0; i < 6; i++ {
fmt.Println(tm.Format("2006-01-02"))
tm = time.Date(tm.Year(), tm.Month()+2, 0, 0, 0, 0, 0, time.UTC)
}
}
2017-01-31
2017-02-28
2017-03-31
2017-04-30
2017-05-31
2017-06-30
tm := time.Date(2016, 1, 31, 0, 0, 0, 0, time.UTC)
2016-01-31
2016-02-29
2016-03-31
2016-04-30
2016-05-31
2016-06-30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment