見出し画像

Notion Formuraを使用して日付をカスタマイズして表示する

Notionでデータベース作成時に、日付のデータがそのままだと扱いづらいのでボード表示用にカスタマイズした時のメモ

完成図

解説

  • test1 日付のみ
    3/9(木) 月/日(曜日)

  • test2 日付+時刻
    3/9(木) 01:00 月/日(曜日) 時刻

  • test3 終了時刻のみ変化
    3/9(木) 00:00 → 08:00 月/日(曜日) 開始時刻 → 終了時刻

  • test4 日付+時刻変化(時間ONで日付のみ変更時も同様)
    3/9(木) 00:00 → 3/10(金) 12:00
    月/日(曜日) 開始時刻 → 月/日(曜日) 終了時刻

  • test5 日付のみ変化(時間OFF)
    3/9(木) → 3/10(金) 月/日(曜日) → 月/日(曜日)

基本的には既存の日付タブの動作に曜日を追加しただけ

使用した関数

関数が長くなったので分割してる関係上、テーブルを2つ使います。

終了日時がある場合は日付から取得して"非表示-終了日"へ、
"出力"で開始日時と結合してます。

"非表示-終了日"

#見やすくしてます コピペは↓のコピペ用コードブロックからお願いします。

if(
    start(prop("日付")) == end(prop("日付")),   #終了日時の有無
        "",             #true-1
        if(             #false-1
            test(format(prop("日付")), "\\d:\\d") == true,      #時刻入力の有無
                if(     #true-2
                    formatDate(start(prop("日付")), "YYYY/MM/DD") == formatDate(end(prop("日付")),"YYYY/MM/DD"),    #日付が一致しているか
                        formatDate(end(prop("日付")), "HH:mm"),         #true-3 時刻のみ記述
                        formatDate(end(prop("日付")), "MM/DD(ddd) HH:mm")       #false-3 日付+時刻記述
                    ),
                formatDate(end(prop("日付")), "MM/DD(ddd)")     #false-2  日付のみ記述
            )
    )

コピペ用

if(start(prop("日付")) == end(prop("日付")), "", if(test(format(prop("日付")), "\\d:\\d") == true, if(formatDate(start(prop("日付")), "YYYY/MM/DD") == formatDate(end(prop("日付")), "YYYY/MM/DD"), formatDate(end(prop("日付")), "HH:mm"), formatDate(end(prop("日付")), "MM/DD(ddd) HH:mm")), formatDate(end(prop("日付")), "MM/DD(ddd)")))

"出力"

#見やすくしてます コピペは↓のコピペ用コードブロックからお願いします。

if(
    test(format(prop("日付")), "\\d:\\d") == true,     #時刻入力してるかどうか
        formatDate(start(prop("日付")), "M/D(ddd) HH:mm"),      #true-1
        formatDate(start(prop("日付")), "M/D(ddd)")         #false-1
    ) + 
if(
    empty(prop("非表示-終了日")) == true,               #終了日時のデータが存在するか
        "",            #true-2
        concat(" → " + prop("非表示-終了日"))       #false-2
    )

コピペ用

if(test(format(prop("日付")), "\\d:\\d") == true, formatDate(start(prop("日付")), "M/D(ddd) HH:mm"), formatDate(start(prop("日付")), "M/D(ddd)")) + if(empty(prop("N-終了日")) == true, "", concat(" → " + prop("N-終了日")))


余談

時刻入力の有無終了日時の有無を関数で判定するのにめっちゃ苦労したのでこのnote書いてます
時刻入力の有無はredditで解決案見つけました。

code

test(format(prop("Date")), "\\d:\\d")

終了日時の有無は開始日時と終了日時が一致するかどうかで判定してます。(内部データ的に入力してない場合は同じ値になっている為)

疲れた!以上!

質問等あれば私のTwitterまで