Getting Specific Time for the Next Day
In Go programming, there are multiple ways to obtain a specific time for the following day, specifying only the hour and minute.
Initially, a common approach involves setting the hour and minute for the current date and adding one day to the resulting Date:
<code class="go">now := time.Now() tomorrow := time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, time.UTC).AddDate(0, 0, 1)</code>
However, a more efficient and succinct solution exists that minimizes calls to time functions:
<code class="go">yyyy, mm, dd := now.Date() tomorrow := time.Date(yyyy, mm, dd+1, 15, 0, 0, 0, now.Location())</code>
This method directly constructs a time.Time for the desired time on the next day without the need for intermediate calculations. Benchmarks show that it is significantly faster than the previous approach, making it the preferred choice for efficiency.
The above is the detailed content of How to Get the Specific Time for Tomorrow in Go?. For more information, please follow other related articles on the PHP Chinese website!