概要
前回でローカル実行環境を構築したが、日付をDBから取得してフォーマットしたところで問題が起きた。
環境 | TimeZone | format(d,'yyyy-MM-dd HH:mm:ss') |
---|---|---|
Azure | UTC | 2024-05-14 00:00:00 |
ローカル | JST | 2024-05-14 09:00:00 |
結論からいうと、TimeZoneのlocaleの問題であった。
ローカル実行時の環境変数TZ
にUTC
を設定することでローカルもAzure上と同じ挙動にできることを確認した。
Return the formatted date string in the given format. The result may vary by locale.
指定された形式で書式設定された日付文字列を返します。結果はロケールによって異なる場合があります。
Node.js TZ より
The TZ environment variable is used to specify the timezone configuration.
TZ 環境変数は、タイムゾーン構成を指定するために使用されます。
設定
ローカル設定ファイルに環境変数を追加する
local.settings.json
{
"IsEncrypted": false,
"Values": {
+ "TZ": "UTC",
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsStorage": "<connection-string>",
"MyBindingConnection": "<binding-connection-string>",
"AzureWebJobs.HttpExample.Disabled": "true"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*",
"CORSCredentials": false
},
"ConnectionStrings": {
"SQLConnectionString": "<sqlclient-connection-string>"
}
}
試したがダメだったもの
ローカル実行時の環境変数にWEBSITE_TIME_ZONEを設定しても、date-fnsのformatの挙動は変わらなかった。