LoginSignup
0
0

More than 3 years have passed since last update.

米国の日付形式からYYYY-MM-DDに変換する

Last updated at Posted at 2020-08-13

日付形式の変換

文字列として取得した米国の日付形式(mm/dd/yy)から yyyy-mm-dd に変換したい。

いろんなパッケージやパースがあるみたいなんだけど、ドンピシャのが見つからなかったので↓のようにして対応した。

package main

import (
    "fmt"
    "strings"
)

func main()  {
    usDate := "8/9/20"
    // => "8/9/20"

    slice := strings.Split(usDate, "/")
    // => [8 9 20]

    date := fmt.Sprintf("20%s-%02s-%02s", slice[2], slice[1], slice[0])
    // => "2020-08-09"

    fmt.Println("yyyy-mm-dd:", date)
    // => yyyy-mm-dd: 2020-09-08
}

他にいい方法を知ってたらおしえてください(切実)

参考

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