Skip to content
Home » How to print yesterday date in Golang

How to print yesterday date in Golang

Learn about How to print yesterday date in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

How to print yesterday date in Golang

// Golang program to print yesterday date

package main

import "fmt"
import "time"

func main() {
	today := time.Now()
	yesterday := today.AddDate(0, 0, -1)

	fmt.Printf("Today    : %02d-%02d-%04d\n", today.Day(), today.Month(), today.Year())
	fmt.Printf("Yesterday : %02d-%02d-%04d", yesterday.Day(), yesterday.Month(), yesterday.Year())
}

In the above program, we imported the fmt package, which contains the files of the fmt package, and then we can use a function related to the fmt package. Here, we got today’s date using the Now() function and subtract 1 day from today’s date using the AddDate() function by specifying -1 as a day value

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
How to demonstrate the hours() function in Golang
How to demonstrate the Minutes() function in Golang