ADVERTISEMENT
ADVERTISEMENT

Golang program to print yesterday's date

Here, we are going to learn how to print yesterday's date in Golang (Go Language)?
Submitted by Nidhi, on March 22, 2021

Problem Solution:

In this program, we will get today's date using time.Now() function and subtract 1 day from today's date and print yesterday's date on the console screen.

Program/Source Code:

The source code to print yesterday's date is given below. The given program is compiled and executed successfully.

// Golang program to print yesterday's 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())
}

Output:

Today    : 22-03-2021
Yesterday : 21-03-2021

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt and time packages to use time and fmt related functions.

In the main() function, 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 and print yesterday's date on the console screen.

Golang Date & Time Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.