How to do date/time comparison

后端 未结 5 1869
一向
一向 2020-12-23 15:25

Is there any options in doing date comparison in Go? I have to sort data based on date and time - independently. So I might allow an object that occurs within a range of dat

相关标签:
5条回答
  • 2020-12-23 16:07

    The following solved my problem of converting string into date

    package main

    import (
        "fmt"
        "time"
    )
    
    func main() {
        value  := "Thu, 05/19/11, 10:47PM"
        // Writing down the way the standard time would look like formatted our way
        layout := "Mon, 01/02/06, 03:04PM"
        t, _ := time.Parse(layout, value)
        fmt.Println(t)
    }
    
    // => "Thu May 19 22:47:00 +0000 2011"
    

    Thanks to paul adam smith

    0 讨论(0)
  • 2020-12-23 16:17

    For comparison between two times use time.Sub()

    // utc life
    loc, _ := time.LoadLocation("UTC")
    
    // setup a start and end time
    createdAt := time.Now().In(loc).Add(1 * time.Hour)
    expiresAt := time.Now().In(loc).Add(4 * time.Hour)
    
    // get the diff
    diff := expiresAt.Sub(createdAt)
    fmt.Printf("Lifespan is %+v", diff)
    

    The program outputs:

    Lifespan is 3h0m0s
    

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

    0 讨论(0)
  • 2020-12-23 16:18

    For case when your interval's end it's date without hours like "from 2017-01-01 to whole day of 2017-01-16" it's better to adjust interval's to 23 hours 59 minutes and 59 seconds like:

    end = end.Add(time.Duration(23*time.Hour) + time.Duration(59*time.Minute) + time.Duration(59*time.Second)) 
    
    if now.After(start) && now.Before(end) {
        ...
    }
    
    0 讨论(0)
  • 2020-12-23 16:18

    Recent protocols prefer usage of RFC3339 per golang time package documentation.

    In general RFC1123Z should be used instead of RFC1123 for servers that insist on that format, and RFC3339 should be preferred for new protocols. RFC822, RFC822Z, RFC1123, and RFC1123Z are useful for formatting; when used with time.Parse they do not accept all the time formats permitted by the RFCs.

    cutOffTime, _ := time.Parse(time.RFC3339, "2017-08-30T13:35:00Z")
    // POSTDATE is a date time field in DB (datastore)
    query := datastore.NewQuery("db").Filter("POSTDATE >=", cutOffTime).
    
    0 讨论(0)
  • 2020-12-23 16:26

    Use the time package to work with time information in Go.

    Time instants can be compared using the Before, After, and Equal methods. The Sub method subtracts two instants, producing a Duration. The Add method adds a Time and a Duration, producing a Time.

    Play example:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func inTimeSpan(start, end, check time.Time) bool {
        return check.After(start) && check.Before(end)
    }
    
    func main() {
        start, _ := time.Parse(time.RFC822, "01 Jan 15 10:00 UTC")
        end, _ := time.Parse(time.RFC822, "01 Jan 16 10:00 UTC")
    
        in, _ := time.Parse(time.RFC822, "01 Jan 15 20:00 UTC")
        out, _ := time.Parse(time.RFC822, "01 Jan 17 10:00 UTC")
    
        if inTimeSpan(start, end, in) {
            fmt.Println(in, "is between", start, "and", end, ".")
        }
    
        if !inTimeSpan(start, end, out) {
            fmt.Println(out, "is not between", start, "and", end, ".")
        }
    }
    
    0 讨论(0)
提交回复
热议问题