0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Goのtime型で過去300分の時間を出力

Posted at

概要

goで過去300分の時間を出力するプログラムです。
goの文字列からtime型への変換で手間取ったため、備忘録ととして残しておきます。

main.go
package main

import (
	"fmt"
	"strconv"
	"time"
)

func main() {
	t:= time.Now()
	fmt.Println(t)

	for i := 1; i < 300; i++ {

		s:= strconv.Itoa(-i)
		// iを文字列型に変換

		m, _ := time.ParseDuration(s+"m")
		// 文字列型を時間に変換
		
		t2 := t.Add(m-time.Minute)
		// 時間を引く
		fmt.Println(t2)
	}
}

#参考元

https://pkg.go.dev/time#example-Duration.Minutes

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?