本文說明在 Azure Resource Manager 範本 (ARM 範本) 中使用日期的函式。
dateTimeAdd
dateTimeAdd(base, duration, [format])
將時間持續時間新增至基底值。 必須是 ISO 8601 格式。
在 Bicep 中,使用 函 dateTimeAdd 式。
參數
| 參數 | 為必填項目 | 類型 | 說明 |
|---|---|---|---|
| 基礎映像 | 是的 | 字串 | 加法的開始日期時間值。 使用 ISO 8601 時間戳格式。 |
| 持續時間 | 是的 | 字串 | 要加入基底的時間值。 它可以是負值。 使用 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 視為非閏年,將一年加到一年的結果是 2024-01-01T00:00:00Z。 相反地,將一年新增至 2024 年初的閏年,結果為 2024-12-31T00:00:00Z,而不是 2025-01-01T00:00:00Z,因為閏年包含 366 天,而不是 365 天。 此外,在2月的第一天增加一個月時,閏年與非閏年之間的區別就變得明顯,導致月份結果不同。
範例
下列範例範本示範新增時間值的不同方式:
{
"$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 | 繩子 | 2023/4/7 下午 2:53:14 |
| subtract9DaysOutput | 繩子 | 2020/3/29 下午 2:53:14 |
| add1HourOutput | 繩子 | 2020/4/7 下午 3:53:14 |
下一個範例範本示範如何設定自動化排程的開始時間:
{
"$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)
將 epoch 時間整數值轉換為 ISO 8601 日期時間。
在 Bicep 中,使用 函 dateTimeFromEpoch 式。
參數
| 參數 | 為必填項目 | 類型 | 說明 |
|---|---|---|---|
| epochTime | 是的 | 整數 (int) | 要轉換成 datetime 字串的 Epoch 時間。 |
返回值
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 日期時間字串轉換為 epoch 時間整數值。
在 Bicep 中,使用 函 dateTimeToEpoch 式。
參數
| 參數 | 為必填項目 | 類型 | 說明 |
|---|---|---|---|
| 日期時間 | 是的 | 字串 | 要轉換成 Epoch 時間的 datetime 字串。 |
返回值
整數,表示 1970 年 1 月 1 日午夜的秒數。
範例
下列範例顯示時間函式的 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 |
下一個範例會使用 epoch 時間值來設定金鑰保存庫中密鑰的到期日:
{
"$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
utcNow(format)
傳回指定格式的目前 (UTC) 日期時間值。 如果未提供任何格式,則會使用 ISO 8601 (yyyyMMddTHHmmssZ) 格式。
此函式只能用於參數的預設值。
在 Bicep 中,使用 函 utcNow 式。
參數
| 參數 | 為必填項目 | 類型 | 說明 |
|---|---|---|---|
| 格式 | 否 | 字串 | 要轉換為字串的 URI 編碼值。 使用 標準格式 或 自定義格式字串。 |
備註
您只能在運算式內使用此函式,以取得參數的預設值。 在 Bicep 檔案中的其他地方使用此函式會傳回錯誤。 在範本的其他部分不允許該函式,因為這會在每次呼叫時傳回不同的值。 使用相同的參數部署相同的範本,並不會確實產生相同的結果。
如果您在先前的部署包含使用 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 範本的結構和語法。