Question:
How to write a more concise code to get an exact time for the following day (tomorrow) with specific hour and minute?
Answer:
Package time provides a better way to achieve this goal. It normalizes the values for month, day, hour, minute, second, and nanosecond during the conversion. Here's an efficient code snippet:
<code class="go">package main import ( "fmt" "time" ) func main() { now := time.Now() fmt.Println(now.Round(0)) yyyy, mm, dd := now.Date() tomorrow := time.Date(yyyy, mm, dd+1, 15, 0, 0, 0, now.Location()) fmt.Println(tomorrow) }</code>
This code is more efficient because it minimizes the number of calls to package time functions and methods. Benchmarks show that it is faster than other approaches.
The above is the detailed content of How to Get Tomorrow\'s Time with a Specific Hour and Minute in Go?. For more information, please follow other related articles on the PHP Chinese website!