【List Formatting】hour と minute を算出する

f:id:tecchan365:20210807144405p:plain

Microsoft Lists の書式設定では、 「年」「月」「日」を取得する関数(getYear , getMonth 、getDate)は用意されていますが、「時間(hour)」「分(minute)」 を取得する関数は用意されていません。

そこで、どうにかして「時間(hour)」「分(minute)」を取得する方法は無いか、考えてみました。

Number($[日付列])

書式設定で Number([$日付列]) とすると、とある数字が表示されます。

f:id:tecchan365:20210807154107p:plain

この数字は、 「1970年1月1日午前0時(UTC)」からの経過ミリ秒数 (UNIX時間) を表しています。
日本は、協定世界時(UTC)より9時間進んでいるため「1970/01/01 09:00」が「0」となります。

f:id:tecchan365:20210807163336p:plain

この Number 関数で得られるミリ秒を利用すると、「時間(hour)」「分(minute)」を算出することができました。

「時間(hour)」を表示する書式設定

日付列の「時間(hour)」を表示する書式設定は、次の通りです。

f:id:tecchan365:20210807170747p:plain

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=floor((Number([$DateTime]) - Number(Date(getYear([$DateTime]) + '/' + (getMonth([$DateTime]) + 1) + '/' + getDate([$DateTime]) ))) / (1000 * 60 * 60))"
}

「分(minute)」を表示する書式設定

日付列の「分(minute)」を表示する書式設定は、次の通りです。

f:id:tecchan365:20210807171259p:plain

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=(((Number([$DateTime]) - Number(Date(getYear([$DateTime]) + '/' + (getMonth([$DateTime]) + 1) + '/' + getDate([$DateTime]) )) - ((floor((Number([$DateTime]) - Number(Date(getYear([$DateTime]) + '/' + (getMonth([$DateTime]) + 1) + '/' + getDate([$DateTime]) ))) / (1000 * 60 * 60))) * (1000 * 60 * 60))) / (1000 * 60)"
}

参考

Microsoft Lists の書式設定で利用できる関数は、次の Microsoft Docs に記載されています。

docs.microsoft.com