|
| 1 | +--- |
| 2 | +title: Template functions - date |
| 3 | +description: Describes the functions to use in an Azure Resource Manager template to work with dates. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 04/06/2020 |
| 6 | +--- |
| 7 | +# Date functions for ARM templates |
| 8 | + |
| 9 | +Resource Manager provides the following functions for working with dates in your Azure Resource Manager (ARM) templates: |
| 10 | + |
| 11 | +* [dateTimeAdd](#datetimeadd) |
| 12 | +* [utcNow](#utcnow) |
| 13 | + |
| 14 | +## dateTimeAdd |
| 15 | + |
| 16 | +`dateTimeAdd(base, duration, [format])` |
| 17 | + |
| 18 | +Adds a time duration to a base datetime value. |
| 19 | + |
| 20 | +### Parameters |
| 21 | + |
| 22 | +| Parameter | Required | Type | Description | |
| 23 | +|:--- |:--- |:--- |:--- | |
| 24 | +| base | Yes | string | The starting datetime value for the addition. Use [ISO 8601 timestamp format](https://en.wikipedia.org/wiki/ISO_8601). | |
| 25 | +| duration | Yes | string | The time value to add to the base. It can be a negative value. Use [ISO 8601 duration format](https://en.wikipedia.org/wiki/ISO_8601#Durations). | |
| 26 | +| format | No | string | The output format for the date time result. If not provided, the format of the base value is used. Use either [standard format strings](https://docs.microsoft.com/dotnet/standard/base-types/standard-date-and-time-format-strings) or [custom format strings](https://docs.microsoft.com/dotnet/standard/base-types/custom-date-and-time-format-strings). | |
| 27 | + |
| 28 | +### Return value |
| 29 | + |
| 30 | +The datetime value that results from adding the duration value to the base value. |
| 31 | + |
| 32 | +### Examples |
| 33 | + |
| 34 | +The following example template shows different ways of adding time values. |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", |
| 39 | + "contentVersion": "1.0.0.0", |
| 40 | + "parameters":{ |
| 41 | + "baseTime":{ |
| 42 | + "type":"string", |
| 43 | + "defaultValue": "[utcNow('u')]" |
| 44 | + } |
| 45 | + }, |
| 46 | + "variables": { |
| 47 | + "add3Years": "[dateTimeAdd(parameters('baseTime'), 'P3Y')]", |
| 48 | + "subtract9Days": "[dateTimeAdd(parameters('baseTime'), '-P9D')]", |
| 49 | + "add1Hour": "[dateTimeAdd(parameters('baseTime'), 'PT1H')]" |
| 50 | + }, |
| 51 | + "resources": [], |
| 52 | + "outputs": { |
| 53 | + "add3Years": { |
| 54 | + "value": "[variables('add3Years')]", |
| 55 | + "type": "string" |
| 56 | + }, |
| 57 | + "subtract9Days": { |
| 58 | + "value": "[variables('subtract9Days')]", |
| 59 | + "type": "string" |
| 60 | + }, |
| 61 | + "add1Hour": { |
| 62 | + "value": "[variables('add1Hour')]", |
| 63 | + "type": "string" |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +When the preceding template is deployed with a base time of `2020-04-07 14:53:14Z`, the output is: |
| 70 | + |
| 71 | +| Name | Type | Value | |
| 72 | +| ---- | ---- | ----- | |
| 73 | +| add3Years | String | 4/7/2023 2:53:14 PM | |
| 74 | +| subtract9Days | String | 3/29/2020 2:53:14 PM | |
| 75 | +| add1Hour | String | 4/7/2020 3:53:14 PM | |
| 76 | + |
| 77 | +The next example template shows how to set the start time for an Automation schedule. |
| 78 | + |
| 79 | +```json |
| 80 | +{ |
| 81 | + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", |
| 82 | + "contentVersion": "1.0.0.0", |
| 83 | + "parameters": { |
| 84 | + "omsAutomationAccountName": { |
| 85 | + "type": "string", |
| 86 | + "defaultValue": "demoAutomation", |
| 87 | + "metadata": { |
| 88 | + "description": "Use an existing Automation account." |
| 89 | + } |
| 90 | + }, |
| 91 | + "scheduleName": { |
| 92 | + "type": "string", |
| 93 | + "defaultValue": "demoSchedule1", |
| 94 | + "metadata": { |
| 95 | + "description": "Name of the new schedule." |
| 96 | + } |
| 97 | + }, |
| 98 | + "baseTime":{ |
| 99 | + "type":"string", |
| 100 | + "defaultValue": "[utcNow('u')]", |
| 101 | + "metadata": { |
| 102 | + "description": "Schedule will start one hour from this time." |
| 103 | + } |
| 104 | + } |
| 105 | + }, |
| 106 | + "variables": { |
| 107 | + "startTime": "[dateTimeAdd(parameters('baseTime'), 'PT1H')]" |
| 108 | + }, |
| 109 | + "resources": [ |
| 110 | + { |
| 111 | + "name": "[concat(parameters('omsAutomationAccountName'), '/', parameters('scheduleName'))]", |
| 112 | + "type": "microsoft.automation/automationAccounts/schedules", |
| 113 | + "apiVersion": "2015-10-31", |
| 114 | + "location": "eastus2", |
| 115 | + "tags": { |
| 116 | + }, |
| 117 | + "properties": { |
| 118 | + "description": "Demo Scheduler", |
| 119 | + "startTime": "[variables('startTime')]", |
| 120 | + "isEnabled": "true", |
| 121 | + "interval": 1, |
| 122 | + "frequency": "hour" |
| 123 | + } |
| 124 | + } |
| 125 | + ], |
| 126 | + "outputs": { |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +## utcNow |
| 132 | + |
| 133 | +`utcNow(format)` |
| 134 | + |
| 135 | +Returns the current (UTC) datetime value in the specified format. If no format is provided, the ISO 8601 (yyyyMMddTHHmmssZ) format is used. **This function can only be used in the default value for a parameter.** |
| 136 | + |
| 137 | +### Parameters |
| 138 | + |
| 139 | +| Parameter | Required | Type | Description | |
| 140 | +|:--- |:--- |:--- |:--- | |
| 141 | +| format |No |string |The URI encoded value to convert to a string. Use either [standard format strings](https://docs.microsoft.com/dotnet/standard/base-types/standard-date-and-time-format-strings) or [custom format strings](https://docs.microsoft.com/dotnet/standard/base-types/custom-date-and-time-format-strings). | |
| 142 | + |
| 143 | +### Remarks |
| 144 | + |
| 145 | +You can only use this function within an expression for the default value of a parameter. Using this function anywhere else in a template returns an error. The function isn't allowed in other parts of the template because it returns a different value each time it's called. Deploying the same template with the same parameters wouldn't reliably produce the same results. |
| 146 | + |
| 147 | +If you use the [option to redeploy an earlier successful deployment](rollback-on-error.md), and the earlier deployment includes a parameter that uses utcNow, the parameter isn't reevaluated. Instead, the parameter value from the earlier deployment is automatically reused in the rollback deployment. |
| 148 | + |
| 149 | +Be careful redeploying a template that relies on the utcNow function for a default value. When you redeploy and don't provide a value for the parameter, the function is reevaluated. If you want to update an existing resource rather than create a new one, pass in the parameter value from the earlier deployment. |
| 150 | + |
| 151 | +### Return value |
| 152 | + |
| 153 | +The current UTC datetime value. |
| 154 | + |
| 155 | +### Examples |
| 156 | + |
| 157 | +The following example template shows different formats for the datetime value. |
| 158 | + |
| 159 | +```json |
| 160 | +{ |
| 161 | + "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", |
| 162 | + "contentVersion": "1.0.0.0", |
| 163 | + "parameters": { |
| 164 | + "utcValue": { |
| 165 | + "type": "string", |
| 166 | + "defaultValue": "[utcNow()]" |
| 167 | + }, |
| 168 | + "utcShortValue": { |
| 169 | + "type": "string", |
| 170 | + "defaultValue": "[utcNow('d')]" |
| 171 | + }, |
| 172 | + "utcCustomValue": { |
| 173 | + "type": "string", |
| 174 | + "defaultValue": "[utcNow('M d')]" |
| 175 | + } |
| 176 | + }, |
| 177 | + "resources": [ |
| 178 | + ], |
| 179 | + "outputs": { |
| 180 | + "utcOutput": { |
| 181 | + "type": "string", |
| 182 | + "value": "[parameters('utcValue')]" |
| 183 | + }, |
| 184 | + "utcShortOutput": { |
| 185 | + "type": "string", |
| 186 | + "value": "[parameters('utcShortValue')]" |
| 187 | + }, |
| 188 | + "utcCustomOutput": { |
| 189 | + "type": "string", |
| 190 | + "value": "[parameters('utcCustomValue')]" |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +The output from the preceding example varies for each deployment but will be similar to: |
| 197 | + |
| 198 | +| Name | Type | Value | |
| 199 | +| ---- | ---- | ----- | |
| 200 | +| utcOutput | string | 20190305T175318Z | |
| 201 | +| utcShortOutput | string | 03/05/2019 | |
| 202 | +| utcCustomOutput | string | 3 5 | |
| 203 | + |
| 204 | +The next example shows how to use a value from the function when setting a tag value. |
| 205 | + |
| 206 | +```json |
| 207 | +{ |
| 208 | + "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", |
| 209 | + "contentVersion": "1.0.0.0", |
| 210 | + "parameters": { |
| 211 | + "utcShort": { |
| 212 | + "type": "string", |
| 213 | + "defaultValue": "[utcNow('d')]" |
| 214 | + }, |
| 215 | + "rgName": { |
| 216 | + "type": "string" |
| 217 | + } |
| 218 | + }, |
| 219 | + "resources": [ |
| 220 | + { |
| 221 | + "type": "Microsoft.Resources/resourceGroups", |
| 222 | + "apiVersion": "2018-05-01", |
| 223 | + "name": "[parameters('rgName')]", |
| 224 | + "location": "westeurope", |
| 225 | + "tags":{ |
| 226 | + "createdDate": "[parameters('utcShort')]" |
| 227 | + }, |
| 228 | + "properties":{} |
| 229 | + } |
| 230 | + ], |
| 231 | + "outputs": { |
| 232 | + "utcShort": { |
| 233 | + "type": "string", |
| 234 | + "value": "[parameters('utcShort')]" |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | +``` |
0 commit comments