この記事では、Azure Resource Manager テンプレート (ARM テンプレート) で日付を操作するための関数について説明します。
dateTimeAdd
dateTimeAdd(base, duration, [format])
基本値に期間を追加します。 ISO 8601 形式が必要です。
Bicep で、 dateTimeAdd 関数を使用します。
パラメーター
| パラメーター | 必須 | タイプ | 説明 |
|---|---|---|---|
| ベース | イエス | 文字列 | 加算の開始日時値。 ISO 8601 タイムスタンプ形式を使用します。 |
| duration | イエス | 文字列 | ベースに追加する時間値。 負の値を指定できます。 ISO 8601 期間形式を使用します。 |
| フォーマット | いいえ | 文字列 | datetime 結果の出力形式。 指定しない場合は、基本値の形式が使用されます。 標準書式文字列またはカスタム書式指定文字列を使用します。 |
戻り値
期間の値を基本値に追加した結果の datetime 値。
注釈
dateTimeAdd関数は閏年を考慮せず、P1Y は P365D として解釈する必要があり、P1M は P30D として解釈する必要があります。 次の json は、いくつかの例を示しています。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"addOneYearNonLeap": {
"type": "string",
"value": "[dateTimeAdd('2023-01-01 00:00:00Z', 'P1Y')]" //2024-01-01T00:00:00Z
},
"addOneYearLeap": {
"type": "string",
"value": "[dateTimeAdd('2024-01-01 00:00:00Z', 'P1Y')]" //2024-12-31T00:00:00Z
},
"addOneMonthNonLeap": {
"type": "string",
"value": "[dateTimeAdd('2023-02-01 00:00:00Z', 'P1M')]" //2023-03-03T00:00:00Z
},
"addOneMonthLeap": {
"type": "string",
"value": "[dateTimeAdd('2024-02-01 00:00:00Z', 'P1M')]" //2024-03-02T00:00:00Z
}
}
}
前の例では、2023 を閏以外の年と考えると、年の最初の日に 1 年を追加した結果は 2024-01-01T00:00:00Z です。 逆に、閏年である 2024 年の開始日に 1 年を追加すると、閏年が 365 日ではなく 366 日で構成されることを考えると、 2025-01-01T00:00:00Z ではなく 2024-12-31T00:00:00Z になります。 さらに、2 月の最初の日に 1 か月を追加すると、閏年と非閏年の区別が明らかになり、月の日の結果が変わります。
例示
次のテンプレート例は、時間値を追加するさまざまな方法を示しています。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"baseTime": {
"type": "string",
"defaultValue": "[utcNow('u')]"
}
},
"variables": {
"add3Years": "[dateTimeAdd(parameters('baseTime'), 'P3Y')]",
"subtract9Days": "[dateTimeAdd(parameters('baseTime'), '-P9D')]",
"add1Hour": "[dateTimeAdd(parameters('baseTime'), 'PT1H')]"
},
"resources": [],
"outputs": {
"add3YearsOutput": {
"value": "[variables('add3Years')]",
"type": "string"
},
"subtract9DaysOutput": {
"value": "[variables('subtract9Days')]",
"type": "string"
},
"add1HourOutput": {
"value": "[variables('add1Hour')]",
"type": "string"
}
}
}
上記のテンプレートが基本時間の 2020-04-07 14:53:14Z でデプロイされた場合、出力は次のようになります。
| 名前 | タイプ | 価値 |
|---|---|---|
| add3YearsOutput | 糸 | 4/7/2023 2:53:14 PM |
| subtract9DaysOutput | 糸 | 3/29/2020 2:53:14 PM |
| add1HourOutput | 糸 | 4/7/2020 3:53:14 PM |
次のテンプレート例は、自動化スケジュールの開始時刻を設定する方法を示しています。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"omsAutomationAccountName": {
"type": "string",
"defaultValue": "demoAutomation",
"metadata": {
"description": "Use an existing Automation account."
}
},
"scheduleName": {
"type": "string",
"defaultValue": "demoSchedule1",
"metadata": {
"description": "Name of the new schedule."
}
},
"baseTime": {
"type": "string",
"defaultValue": "[utcNow('u')]",
"metadata": {
"description": "Schedule will start one hour from this time."
}
}
},
"variables": {
"startTime": "[dateTimeAdd(parameters('baseTime'), 'PT1H')]"
},
"resources": [
...
{
"type": "Microsoft.Automation/automationAccounts/schedules",
"apiVersion": "2024-10-23",
"name": "[concat(parameters('omsAutomationAccountName'), '/', parameters('scheduleName'))]",
"properties": {
"description": "Demo Scheduler",
"startTime": "[variables('startTime')]",
"interval": 1,
"frequency": "Hour"
}
}
],
"outputs": {
}
}
dateTimeFromEpoch
dateTimeFromEpoch(epochTime)
エポック時間整数値を ISO 8601 datetime に変換します。
Bicep で、 dateTimeFromEpoch 関数を使用します。
パラメーター
| パラメーター | 必須 | タイプ | 説明 |
|---|---|---|---|
| epochTime | イエス | 整数 (int) | datetime 文字列に変換するエポック時間。 |
戻り値
ISO 8601 datetime 文字列。
例
次の例は、 epoch 時間関数の出力値を示しています。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"convertedEpoch": {
"type": "int",
"defaultValue": "[dateTimeToEpoch(dateTimeAdd(utcNow(), 'P1Y'))]"
}
},
"variables": {
"convertedDatetime": "[dateTimeFromEpoch(parameters('convertedEpoch'))]"
},
"resources": [],
"outputs": {
"epochValue": {
"type": "int",
"value": "[parameters('convertedEpoch')]"
},
"datetimeValue": {
"type": "string",
"value": "[variables('convertedDatetime')]"
}
}
}
出力は次のようになります。
| 名前 | タイプ | 価値 |
|---|---|---|
| datetimeValue | 糸 | 2023-05-02T15:16:13Z |
| epochValue | 整数 | 1683040573 |
dateTimeToEpoch
dateTimeToEpoch(dateTime)
ISO 8601 datetime 文字列をエポック時間整数値に変換します。
Bicep で、 dateTimeToEpoch 関数を使用します。
パラメーター
| パラメーター | 必須 | タイプ | 説明 |
|---|---|---|---|
| 日時 | イエス | 文字列 | エポック時間に変換する datetime 文字列。 |
戻り値
1970 年 1 月 1 日の午前 0 時からの秒数を表す整数。
例示
次の例は、 epoch 時間関数の出力値を示しています。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"convertedEpoch": {
"type": "int",
"defaultValue": "[dateTimeToEpoch(dateTimeAdd(utcNow(), 'P1Y'))]"
}
},
"variables": {
"convertedDatetime": "[dateTimeFromEpoch(parameters('convertedEpoch'))]"
},
"resources": [],
"outputs": {
"epochValue": {
"type": "int",
"value": "[parameters('convertedEpoch')]"
},
"datetimeValue": {
"type": "string",
"value": "[variables('convertedDatetime')]"
}
}
}
出力は次のようになります。
| 名前 | タイプ | 価値 |
|---|---|---|
| datetimeValue | 糸 | 2023-05-02T15:16:13Z |
| epochValue | 整数 | 1683040573 |
次の例では、エポック時間値を使用して、キー コンテナー内のキーの有効期限を設定します。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.5.6.12127",
"templateHash": "16023511331197397029"
}
},
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "The location into which the resources should be deployed."
}
},
"tenantId": {
"type": "string",
"defaultValue": "[subscription().tenantId]",
"metadata": {
"description": "The Tenant Id that should be used throughout the deployment."
}
},
"userAssignedIdentityName": {
"type": "string",
"metadata": {
"description": "The name of the existing User Assigned Identity."
}
},
"userAssignedIdentityResourceGroupName": {
"type": "string",
"metadata": {
"description": "The name of the resource group for the User Assigned Identity."
}
},
"keyVaultName": {
"type": "string",
"defaultValue": "[format('vault-{0}', uniqueString(resourceGroup().id))]",
"metadata": {
"description": "The name of the Key Vault."
}
},
"keyVaultKeyName": {
"type": "string",
"defaultValue": "cmkey",
"metadata": {
"description": "Name of the key in the Key Vault"
}
},
"keyExpiration": {
"type": "int",
"defaultValue": "[dateTimeToEpoch(dateTimeAdd(utcNow(), 'P1Y'))]",
"metadata": {
"description": "Expiration time of the key"
}
},
"storageAccountName": {
"type": "string",
"defaultValue": "[format('storage{0}', uniqueString(resourceGroup().id))]",
"metadata": {
"description": "The name of the Storage Account"
}
}
},
"resources": [
{
"type": "Microsoft.KeyVault/vaults",
"apiVersion": "2021-10-01",
"name": "[parameters('keyVaultName')]",
"location": "[parameters('location')]",
"properties": {
"sku": {
"name": "standard",
"family": "A"
},
"enableSoftDelete": true,
"enablePurgeProtection": true,
"enabledForDiskEncryption": true,
"tenantId": "[parameters('tenantId')]",
"accessPolicies": [
{
"tenantId": "[parameters('tenantId')]",
"permissions": {
"keys": [
"unwrapKey",
"wrapKey",
"get"
]
},
"objectId": "[reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, parameters('userAssignedIdentityResourceGroupName')), 'Microsoft.ManagedIdentity/userAssignedIdentities', parameters('userAssignedIdentityName')), '2018-11-30').principalId]"
}
]
}
},
{
"type": "Microsoft.KeyVault/vaults/keys",
"apiVersion": "2021-10-01",
"name": "[format('{0}/{1}', parameters('keyVaultName'), parameters('keyVaultKeyName'))]",
"properties": {
"attributes": {
"enabled": true,
"exp": "[parameters('keyExpiration')]"
},
"keySize": 4096,
"kty": "RSA"
},
"dependsOn": [
"[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]"
]
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"[format('{0}', extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, parameters('userAssignedIdentityResourceGroupName')), 'Microsoft.ManagedIdentity/userAssignedIdentities', parameters('userAssignedIdentityName')))]": {}
}
},
"properties": {
"accessTier": "Hot",
"supportsHttpsTrafficOnly": true,
"minimumTlsVersion": "TLS1_2",
"encryption": {
"identity": {
"userAssignedIdentity": "[extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, parameters('userAssignedIdentityResourceGroupName')), 'Microsoft.ManagedIdentity/userAssignedIdentities', parameters('userAssignedIdentityName'))]"
},
"services": {
"blob": {
"enabled": true
}
},
"keySource": "Microsoft.Keyvault",
"keyvaultproperties": {
"keyname": "[parameters('keyVaultKeyName')]",
"keyvaulturi": "[if(endsWith(reference(resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))).vaultUri, '/'), substring(reference(resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))).vaultUri, 0, sub(length(reference(resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))).vaultUri), 1)), reference(resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))).vaultUri)]"
}
}
},
"dependsOn": [
"[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]",
"[resourceId('Microsoft.KeyVault/vaults/keys', parameters('keyVaultName'), parameters('keyVaultKeyName'))]"
]
}
]
}
utcNow(現在のUTC時間を取得する機能)
utcNow(format)
指定した形式の現在の (UTC) datetime 値を返します。 形式が指定されていない場合は、ISO 8601 (yyyyMMddTHHmmssZ) 形式が使用されます。
この関数は、パラメーターの既定値でのみ使用できます。
Bicep で、 utcNow 関数を使用します。
パラメーター
| パラメーター | 必須 | タイプ | 説明 |
|---|---|---|---|
| フォーマット | いいえ | 文字列 | 文字列に変換する URI エンコードされた値。 標準書式文字列またはカスタム書式指定文字列を使用します。 |
注釈
この関数は、パラメーターの既定値に対する式の中でのみ使用できます。 この関数をテンプレートのその他の場所で使用すると、エラーが返されます。 テンプレートの他の場所でこの関数を使用することは、呼び出しのたびに異なる値が返されるため、許可されていません。 同じパラメーターで同じテンプレートをデプロイしても、同じ結果が生成される保証はありません。
以前のデプロイに utcNow を使用するパラメーターが含まれている場合に、エラー時にロールバックするオプションを使用した場合、パラメーターは再評価されません。 代わりに、以前のデプロイのパラメーター値が、ロールバック デプロイで自動的に再利用されます。
既定値の utcNow 関数に依存するテンプレートを再デプロイする場合は注意してください。 再デプロイを行うときに、パラメーターの値を指定しないと、関数が再評価されます。 新しいリソースを作成するのではなく、既存のリソースを更新する場合は、以前のデプロイのパラメーター値を渡します。
戻り値
現在の UTC 日時 値。
例示
次のテンプレート例は、datetime 値のさまざまな形式を示しています。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"utcValue": {
"type": "string",
"defaultValue": "[utcNow()]"
},
"utcShortValue": {
"type": "string",
"defaultValue": "[utcNow('d')]"
},
"utcCustomValue": {
"type": "string",
"defaultValue": "[utcNow('M d')]"
}
},
"resources": [
],
"outputs": {
"utcOutput": {
"type": "string",
"value": "[parameters('utcValue')]"
},
"utcShortOutput": {
"type": "string",
"value": "[parameters('utcShortValue')]"
},
"utcCustomOutput": {
"type": "string",
"value": "[parameters('utcCustomValue')]"
}
}
}
前の例からの出力はデプロイごとに変わりますが、次のようになります。
| 名前 | タイプ | 価値 |
|---|---|---|
| utcOutput | 文字列 | 20190305T175318Z |
| utcShortOutput | 文字列 | 03/05/2019 |
| utcCustomOutput | 文字列 | 3 5 |
次の例は、タグ値を設定するときに関数の値を使用する方法を示しています。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"utcShort": {
"type": "string",
"defaultValue": "[utcNow('d')]"
},
"rgName": {
"type": "string"
}
},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2025-04-01",
"name": "[parameters('rgName')]",
"location": "westeurope",
"tags": {
"createdDate": "[parameters('utcShort')]"
},
"properties": {}
}
],
"outputs": {
"utcShortOutput": {
"type": "string",
"value": "[parameters('utcShort')]"
}
}
}
次のステップ
ARM テンプレートのセクションの詳細については、ARM テンプレート の構造と構文を参照してください。