获得一个月的最后一天。时间

时间:2016-02-03 16:32:24

标签: time go

当我有time.Time

// January, 29th
t, _ := time.Parse("2006-01-02", "2016-01-29")

如何获得代表1月31日的time.Time?这个例子很简单,但是当二月有一个日期时,最后一天可能是28日或29日。

4 个答案:

答案 0 :(得分:6)

  

Package time

     

func Date

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
     

日期返回与

对应的时间      

yyyy-mm-dd hh:mm:ss + nsec纳秒

     

在指定地点的适当区域。

     

月,日,小时,分钟,秒和nsec值可能超出其范围   通常的范围,并将在转换过程中标准化。对于   例如,10月32日转换为11月1日。

例如,规范日期

package main

import (
    "fmt"
    "time"
)

func main() {
    // January, 29th
    t, _ := time.Parse("2006-01-02", "2016-01-29")
    fmt.Println(t.Date())
    // January, 31st
    y,m,_ := t.Date()
    lastday:= time.Date(y,m+1,0,0,0,0,0,time.UTC)
    fmt.Println(lastday.Date())
}

输出:

2016 January 29
2016 January 31

答案 1 :(得分:2)

你可以自己写一个函数,也许是这样的:

func daysInMonth(month, year int) int {
    switch time.Month(month) {
    case time.April, time.June, time.September, time.November:
        return 30
    case time.February:
        if year%4 == 0 && (year%100 != 0 || year%400 == 0) { // leap year
            return 29
        }
        return 28
    default: 
    return 31
    }
}
编辑:因为我非常喜欢测量事物:

$ go test -bench .
testing: warning: no tests to run
PASS
BenchmarkDim2-8         200000000                7.26 ns/op
BenchmarkDim-8          1000000000               2.80 ns/op // LIES!
BenchmarkTime-8         10000000               169 ns/op
BenchmarkTime2-8        10000000               234 ns/op
ok      github.com/drathier/scratchpad/go       9.741s

BenchMarkDim2:未经测试,但非常快。

func daysInMonthTime(month, year int) time.Time {
    return time.Time{}.Add(time.Hour * 10 + time.Hour*24*30*time.Duration(month-1) + time.Second * time.Duration(daysInMonth(month, year)) * 24 * 60 + 1337)
}

BenchmarkDim:// LIES

func daysInMonth(month, year int) int {
    switch time.Month(month) {
    case time.April, time.June, time.September, time.November:
        return 30
    case time.February:
        if year%4 == 0 && (year%100 != 0 || year%400 == 0) {
            // leap year
            return 29
        }
        return 28
    default:
        return 31
    }
}

BenchmarkTime:

func timeDaysInMonth() time.Time {
    // January, 29th
    t, _ := time.Parse("2006-01-02", "2016-01-29")
    y, m, _ := t.Date()
    lastday := time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC)
    return lastday
}

BenchmarkTime2

func time2daysinmonth() time.Time {
    t, _ := time.Parse("2006-01-02", "2016-01-01")
    t = t.AddDate(0, 1, 0).AddDate(0, 0, -1)
    return t
}

答案 2 :(得分:1)

我在自己的代码中使用了类似的东西:

func lastDayOfTheMonth(year, month int) time.Time {
    if month++; month > 12 {
        month = 1
    }
    t := time.Date(year, time.Month(month), 0, 0, 0, 0, 0, time.UTC)
    return t
}

playground

答案 3 :(得分:0)

这不是特定的,但通常我会用任何语言进行跟踪:

package main

import "fmt"
import "time"

func main() {
    fmt.Println("Hello, playground")

    t, _ := time.Parse("2006-01-02", "2016-01-01")
    t = t.AddDate(0, 1, 0).AddDate(0,0,-1)
    fmt.Printf("Last day: %v\n", t)
}

http://play.golang.org/p/JhpOZvEhBw

相关问题