4
2

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.

Python シリアル値 日付変換

Last updated at Posted at 2021-10-10

時々使用するシリアル値(例:44434.3412172569)から、
日付データ(yyyy/mm/dd HH:MM:SS)に変換する方法のメモです。

個人的に利用する頻度が高いので、備忘として残しておきます。

シリアル値とは

超シンプルに言うと、「1900/1/1」から何日経過したかを数値で扱ったものです。
24時間を「1」として、「1900/1/1」からの経過日時を算出しています。

(シリアル値はExcelなどでもよく見かけます。)

ソースコード

下記コードをコピペで利用可能です。

def SerialToDateTime(serialVal:float) -> str:
    """日付シリアル値からyyyy/MM/dd HH:MM:ss 形式に変換
    Args:
        serialVal (float): シリアル値(Ex: 44434.3412172569)
    Returns:
        str: 時刻  yyyy/MM/dd HH:MM:ss 形式
    """      
    sDateTime = (datetime(1899,12,30) + timedelta(serialVal)).strftime('%Y/%m/%d %H:%M:%S')

    return sDateTime

以上、簡単ですがシリアル値から日付データに変更する方法でした!

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?