dongzouhe9734 2017-11-16 12:31
浏览 28
已采纳

如何获得最后一个季度

Here below is my code to get the last complete quarter:

package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "2006-01-02T15:04:05.000Z"
    str := "2017-11-30T12:00:00.000Z"
    now, _ := time.Parse(layout, str)

    endDate := now.AddDate(0, 0, 0-now.Day())
    startDate := endDate.AddDate(0, -3, 0) // startDate is wrong: 2017-07-31
    // the following statement is needed to fix startDate
    if endDate.Month()-startDate.Month() == 3 {
        startDate = startDate.AddDate(0, 0, 1) // now startDate is correct: 2017-08-01
    }

    fmt.Printf("Start date: %v
", startDate.Format("2006-01-02"))
    fmt.Printf("End date: %v
", endDate.Format("2006-01-02"))
}

<kbd>playground</kbd>

Is there a better way to get the correct start date?

For instance, the last startDate = startDate.AddDate(0, 0, 1) statement has to be omitted if I want to get the last semester:

endDate := now.AddDate(0, 0, 0-now.Day())
startDate := endDate.AddDate(0, -6, 0) // startDate is correct: 2017-05-01

Why is there this difference?

  • 写回答

1条回答 默认 最新

  • douba05167 2017-11-16 15:10
    关注

    Package time

    import "time"
    

    func Date

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

    Date returns the Time corresponding to

    yyyy-mm-dd hh:mm:ss + nsec nanoseconds
    

    in the appropriate zone for that time in the given location.

    The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.


    For example, using normalization to get the last complete period (for example, quarter or semester):

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func lastPeriod(t time.Time, period time.Month) (start, end time.Time) {
        y, m, _ := t.Date()
        loc := t.Location()
        start = time.Date(y, m-period, 1, 0, 0, 0, 0, loc)
        end = time.Date(y, m, 1, 0, 0, 0, -1, loc)
        return start, end
    }
    
    func main() {
        layout := "2006-01-02T15:04:05.000Z"
        str := "2017-11-30T12:00:00.000Z"
        now, err := time.Parse(layout, str)
        if err != nil {
            fmt.Println(err)
            return
        }
        const (
            quarter  = 3
            semester = 6
        )
        fmt.Println("Quarter:")
        start, end := lastPeriod(now, quarter)
        fmt.Printf("Base date:  %v
    ", now.Format("2006-01-02"))
        fmt.Printf("Start date: %v
    ", start.Format("2006-01-02"))
        fmt.Printf("End date:   %v
    ", end.Format("2006-01-02"))
        fmt.Println("Semester:")
        start, end = lastPeriod(now, semester)
        fmt.Printf("Base date:  %v
    ", now.Format("2006-01-02"))
        fmt.Printf("Start date: %v
    ", start.Format("2006-01-02"))
        fmt.Printf("End date:   %v
    ", end.Format("2006-01-02"))
    }
    

    Playground: https://play.golang.org/p/0t4exjVgr-

    Output:

    Quarter:
    Base date:  2017-11-30
    Start date: 2017-08-01
    End date:   2017-10-31
    Semester:
    Base date:  2017-11-30
    Start date: 2017-05-01
    End date:   2017-10-31
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?