LoginSignup
1
1

More than 5 years have passed since last update.

Excelの日付のFloat値

Last updated at Posted at 2018-11-08

Excelで日時を標準にしたらFloat値が出てくる。

image.png
このFloat値はどういう値なのか?

整数部は日付

1990年1月0日からの経過日数。1990年1月1日だと1になる。
ただし1990年2月29日という存在しない日付もカウントしてる。
なので2018年11月8日は43412日経過している。

小数部は時刻

セルの時刻部(14:51)を秒数にしたものを24h x 60min x 60sec => 86400secで割ったもの。

14:51:00 = 14 * (60 * 60) + 51 * 60 = 53460
53460 / 86400 = 0.61875 (最初のスクショの小数部に一致!)

Floatから日時を復元する

ExcelをGoで読みとる際に日付変換したかったので、以下の様に解決しました。

func convertExcelDateFloat(excelDateFloat float64, dateFormat string) string {
    pastDay := int(excelDateFloat)
    pastSec := excelDateFloat - float64(pastDay)
    date := time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local)

    // Excelでは 1990/1/0 を開始基準として経過日数を整数部で数えている。つまり1990/1/1は1日経過している。
    // 加えて存在しない 1990/2/29 も経過日数に含まれているので-2日を差し引いた経過日数を足す
    date = date.Add(time.Duration(pastDay-2) * time.Hour * 24)

    // 小数部は時刻部を秒変換したのものを24時間秒(24h * 60m * 60s = 86400s)で割ったものとなっている
    // なので"小数部 * 86400 = 経過秒"に復元したものを足す
    date = date.Add(time.Duration(pastSec*86400) * time.Second)
    return date.Format(dateFormat)
}
1
1
2

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
1
1