2016-02-03 28 views
5

的月當我有一個time.Time獲取最後一天了time.time

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

我怎樣才能得到一個time.Time代表1月31日?這個例子是微不足道的,但是如果在2月有一個日期,那麼最後一天可能是28日或29日。

回答

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值可能超出其常規範圍,並且在轉換過程中將被歸一化。對於 例如,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 
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://謊言

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 
} 
+0

的問題:「我怎樣才能得到一個'time.Time'代表1月31日?「你的基準是無效的。他們有不同的投入和產出。他們不做同樣的事情。解釋:有謊言,該死的謊言和基準! – peterSO

+0

哈哈,你說得對。更新:D –

+1

(您的答案應該是公認的答案;可讀性是關鍵,即使它解決了比問題要求更難的問題,例如保持跟蹤當前年份) –

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

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

相關問題