K
K
kvaks2021-01-12 11:07:35
go
kvaks, 2021-01-12 11:07:35

How to compare time?

Tell me how to compare time correctly.
I was able to do this, but the error is +3 hours.

var dateStart = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 9,  0, 0, 0, time.UTC)
var dateEnd   = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 17, 0, 0, 0, time.UTC)

  if dateStart.Before(time.Now()){
    println("star ok")
  }
  if	dateEnd.After(time.Now()) {
    println("end ok")
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-01-12
@kvaks

You are creating the time in UTC zone, and when you call time.Now() it returns you the time in your zone, not in UTC.
Just add a UTC() call to time.Now().UTC()

var dateStart = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 9,  0, 0, 0, time.UTC)
var dateEnd   = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 17, 0, 0, 0, time.UTC)

  if dateStart.Before(time.Now().UTC() ){
    println("star ok")
  }
  if dateEnd.After(time.Now().UTC()) {
    println("end ok")
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question