Home > Backend Development > Golang > How to determine whether the time is the previous day in Go language?

How to determine whether the time is the previous day in Go language?

WBOY
Release: 2024-03-24 11:54:04
Original
531 people have browsed it

How to determine whether the time is the previous day in Go language?

How to determine whether the time is the previous day in Go language?

In the Go language, we often need to operate and judge dates and times. Sometimes we need to determine whether a given time is the previous day, which is very common in some demand scenarios. This article will introduce how to determine whether the time is the previous day in Go language and provide specific code examples.

First, we need to import the "time" package in the Go language standard library to facilitate time operations. The code is as follows:

import (
    "time"
)
Copy after login

Next, we need to write a function that accepts a parameter of type time.Time, and then determines whether the time is the previous day. The following is a complete code example:

package main

import (
    "fmt"
    "time"
)

func isYesterday(t time.Time) bool {
    now := time.Now()
    yesterday := now.AddDate(0, 0, -1)

    year, month, day := t.Date()
    yearNow, monthNow, dayNow := yesterday.Date()

    if year == yearNow && month == monthNow && day == dayNow {
        return true
    }

    return false
}

func main() {
    t := time.Date(2022, time.September, 21, 0, 0, 0, 0, time.UTC)
    if isYesterday(t) {
        fmt.Println("给定的时间是昨天!")
    } else {
        fmt.Println("给定的时间不是昨天!")
    }
}
Copy after login

In the above code, we define a function named isYesterday, which accepts a parameter t of type time.Time and returns a Boolean value indicating that Whether the specified time is the previous day. We first get the current time now, and then get yesterday's time yesterday through now.AddDate(0, 0, -1). Next, we compare the parameter t with the year, month, and day of yesterday. If they are equal, it means that the given time is the previous day and return true; otherwise, return false.

In the main function, we create a time t, and then call the isYesterday function to determine whether the time is the previous day and output the result.

Summary: Through the introduction and code examples of this article, you should now know how to determine whether the time is the previous day in Go language. Hope this helps!

The above is the detailed content of How to determine whether the time is the previous day in Go language?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template