I am doing program for one of checking I need real time in minutes like if its 10:00 I should have 600 minutes
I tried using time.Now() function and .Hour and .Minute and it works but sadly when it comes to ParseDuration to get minutes of hours I get these errors : go:91:4: assignment mismatch: 1 variable but time.ParseDuration returns 2 values go:91:25: cannot use hours (type func() int) as type string in argument to time.ParseDuration
now := time.Now()
hours := (now.Hour)
minutes := (now.Minute)
m := time.ParseDuration(hours)
I expect get real time in minutes as integer that i could use in my if statements and cycles
First, convert your time object to an interval. It sounds like what you care about is hours/minutes since midnight, so refer to this post to calculate midnight for your local time, or UTC, whichever you care about. Then determine the duration since midnight, the you can easily use the Hours
or Minutes
methods on the Duration type.
Putting it all together:
midnight := time.Now().Truncate(24 * time.Hour) // or similar, depending on exact needs
dur := time.Since(midnight)
hours := dur.Hours()
mins := dur.Minutes()
Note that Hours()
and Minutes()
return floats, not integers. If you need integers, do your own conversion.
The time.Time
object also has the Hour
and Minute
methods, which may do what you want, but they give less control when it comes to timezone handling.
now := time.Now()
hour := now.Hour()
min := now.Minute()