Case: You want to make a report for your boss. And you make a simple reporting graphics. The data is H-1 from today and 30 days from H-1 day and make cool graphic chart from it.
Resolution:
package mainimport ( "fmt" "time")func main() { today := time.Now() fmt.Println("Today is : ", today.Format("2006-01-02 03:04:00pm")) // loop until 30 days before from today for i := 1; i<=30; i++ { // do whatever you want here ... d := today.AddDate(0, 0, -i) fmt.Println(d.Format("2006-01-02")) }}
Result:
Today is : 2009-11-10 11:00:00pm2009-11-092009-11-082009-11-072009-11-062009-11-052009-11-042009-11-032009-11-022009-11-012009-10-312009-10-302009-10-292009-10-282009-10-272009-10-262009-10-252009-10-242009-10-232009-10-222009-10-212009-10-202009-10-192009-10-182009-10-172009-10-162009-10-152009-10-142009-10-132009-10-122009-10-11
Play in the playground:
https://play.golang.org/p/cFkVSa9sy48
Originally posted 2019-09-02 07:44:40.