How do I get the Unix epoch in seconds for a date without time?

Issue

In Go, how do I get the Unix epoch in seconds for a date without time?

For example, if I want the Unix epoch seconds for the current time, I can use:

today := time.Now().Unix()

For instance, if the current date+time is 2021-12-31 11:56:01, I want to get the Unix epoch in seconds for 2021-12-31 00:00:00. Is there an easy way to do this, or do I have to convert to a string, manipulate the string, then parse the string?

Solution

You can use time.Truncate for rounding the time to the full day:

today_00 := time.Now().Truncate(24*time.Hour).Unix()

Answered By – Steffen Ullrich

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.