How to get time in seconds with add time zone in go


I am using time.Time in go. to get time in seconds (the number of seconds elapsed since 1970’s January 1) I am using

now :=
loc, err := time.LoadLocation(country.Timezone) // time zone in Asia/Dhaka
now = now.In(loc)


seconds := now.Unix()

but the seconds giving seconds without adding the time zone seconds. it actually giving the seconds in UTC. My question is, how can I get seconds with added 6 hours ( asia/dhaka time zone is UTC+6)?


If you want current clock time’s second part only use below code:

loc := time.FixedZone("some_common_name", 6*60*60)
ts := time.Now().In(loc).Second()

If you want seconds from start of current year(like 01.01.1970)

loc := time.FixedZone("some_common_name", 6*60*60)
tp := time.Date(1970, 1, 1, 0, 0, 0, 0, loc)
ts := time.Now().Sub(tp).Seconds()
fmt.Printf("%v", ts)

In case, you want different time zone information, change offset value of time.FixedZone() functions. As, if you want GMT +5, then use 5*60*60 as offset value

Answered By – Abu Hanifa

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.