irom
irom

Reputation: 3606

How to add 1 sec to the date in golang?

How to add 1 sec to the date in golang ? I have:

t := time.Now().Format("2006/02/01 03:04:05")

and want something like below but so far getting mismatched types string and time.Duration error

t1, t2, t3 = t + 1*time.Second, t+3*time.Second, t+2*time.Second

Upvotes: 2

Views: 11284

Answers (2)

solarc
solarc

Reputation: 5738

You are asigning a string to t (the result of calling Format) instead of a Time (the result of calling Now). Here's an working example:

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Println(t.Format(time.RFC3339))

    t = t.Add(time.Second)
    fmt.Println(t.Format(time.RFC3339))
}

// prints
// 2017-01-21T16:51:31-05:00
// 2017-01-21T16:51:32-05:00

Upvotes: 4

Jakub M.
Jakub M.

Reputation: 33867

func (t Time) Add(d Duration) Time

https://golang.org/pkg/time/#Time.Add

Upvotes: 6

Related Questions