Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get yesterday's date in golang?

Tags:

time

go

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

like image 749
saggaf.arsyad Avatar asked Jun 08 '16 08:06

saggaf.arsyad


Video Answer


1 Answers

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.

like image 150
cnicutar Avatar answered Sep 25 '22 17:09

cnicutar