saggaf.arsyad
saggaf.arsyad

Reputation: 831

How to get yesterday's date in golang?

How do I get yesterday's date in the time.Time struct in Go?

Upvotes: 76

Views: 65439

Answers (1)

cnicutar
cnicutar

Reputation: 182629

Here's one way with AddDate:

time.Now().AddDate(0, 0, -1)

EDIT

The original answer also had a time.Add suggestion:

fmt.Printf("Yesterday: %v\n", time.Now().Add(-24*time.Hour))

See Vatine's comment for reasons to prefer AddDate.

Upvotes: 172

Related Questions