Skip to content

Commit 2b651f6

Browse files
committed
add dateTimeAdd
1 parent 1deedd4 commit 2b651f6

File tree

4 files changed

+195
-113
lines changed

4 files changed

+195
-113
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
# String functions for ARM templates
8+
9+
Resource Manager provides the following functions for working with strings 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 ([ISO 8601 timestamp format](https://en.wikipedia.org/wiki/ISO_8601)) | The starting datetime value for the addition. |
25+
| duration | Yes | string ([ISO 8601 duration format](https://en.wikipedia.org/wiki/ISO_8601#Durations)) | The time value to add to the base. It can be a negative value. |
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+
## utcNow
78+
79+
`utcNow(format)`
80+
81+
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.**
82+
83+
### Parameters
84+
85+
| Parameter | Required | Type | Description |
86+
|:--- |:--- |:--- |:--- |
87+
| 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). |
88+
89+
### Remarks
90+
91+
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.
92+
93+
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.
94+
95+
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.
96+
97+
### Return value
98+
99+
The current UTC datetime value.
100+
101+
### Examples
102+
103+
The following example template shows different formats for the datetime value.
104+
105+
```json
106+
{
107+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
108+
"contentVersion": "1.0.0.0",
109+
"parameters": {
110+
"utcValue": {
111+
"type": "string",
112+
"defaultValue": "[utcNow()]"
113+
},
114+
"utcShortValue": {
115+
"type": "string",
116+
"defaultValue": "[utcNow('d')]"
117+
},
118+
"utcCustomValue": {
119+
"type": "string",
120+
"defaultValue": "[utcNow('M d')]"
121+
}
122+
},
123+
"resources": [
124+
],
125+
"outputs": {
126+
"utcOutput": {
127+
"type": "string",
128+
"value": "[parameters('utcValue')]"
129+
},
130+
"utcShortOutput": {
131+
"type": "string",
132+
"value": "[parameters('utcShortValue')]"
133+
},
134+
"utcCustomOutput": {
135+
"type": "string",
136+
"value": "[parameters('utcCustomValue')]"
137+
}
138+
}
139+
}
140+
```
141+
142+
The output from the preceding example varies for each deployment but will be similar to:
143+
144+
| Name | Type | Value |
145+
| ---- | ---- | ----- |
146+
| utcOutput | string | 20190305T175318Z |
147+
| utcShortOutput | string | 03/05/2019 |
148+
| utcCustomOutput | string | 3 5 |
149+
150+
The next example shows how to use a value from the function when setting a tag value.
151+
152+
```json
153+
{
154+
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
155+
"contentVersion": "1.0.0.0",
156+
"parameters": {
157+
"utcShort": {
158+
"type": "string",
159+
"defaultValue": "[utcNow('d')]"
160+
},
161+
"rgName": {
162+
"type": "string"
163+
}
164+
},
165+
"resources": [
166+
{
167+
"type": "Microsoft.Resources/resourceGroups",
168+
"apiVersion": "2018-05-01",
169+
"name": "[parameters('rgName')]",
170+
"location": "westeurope",
171+
"tags":{
172+
"createdDate": "[parameters('utcShort')]"
173+
},
174+
"properties":{}
175+
}
176+
],
177+
"outputs": {
178+
"utcShort": {
179+
"type": "string",
180+
"value": "[parameters('utcShort')]"
181+
}
182+
}
183+
}
184+
```

articles/azure-resource-manager/templates/template-functions-string.md

Lines changed: 1 addition & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Template functions - string
33
description: Describes the functions to use in an Azure Resource Manager template to work with strings.
44
ms.topic: conceptual
5-
ms.date: 07/31/2019
5+
ms.date: 04/06/2020
66
---
77
# String functions for ARM templates
88

@@ -40,7 +40,6 @@ Resource Manager provides the following functions for working with strings in yo
4040
* [uri](#uri)
4141
* [uriComponent](#uricomponent)
4242
* [uriComponentToString](#uricomponenttostring)
43-
* [utcNow](#utcnow)
4443

4544
## base64
4645

@@ -2095,115 +2094,6 @@ The output from the preceding example with the default values is:
20952094
| componentOutput | String | `http%3A%2F%2Fcontoso.com%2Fresources%2Fnested%2Fazuredeploy.json` |
20962095
| toStringOutput | String | `http://contoso.com/resources/nested/azuredeploy.json` |
20972096

2098-
## utcNow
2099-
2100-
`utcNow(format)`
2101-
2102-
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.**
2103-
2104-
### Parameters
2105-
2106-
| Parameter | Required | Type | Description |
2107-
|:--- |:--- |:--- |:--- |
2108-
| 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). |
2109-
2110-
### Remarks
2111-
2112-
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.
2113-
2114-
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.
2115-
2116-
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.
2117-
2118-
### Return value
2119-
2120-
The current UTC datetime value.
2121-
2122-
### Examples
2123-
2124-
The following example template shows different formats for the datetime value.
2125-
2126-
```json
2127-
{
2128-
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
2129-
"contentVersion": "1.0.0.0",
2130-
"parameters": {
2131-
"utcValue": {
2132-
"type": "string",
2133-
"defaultValue": "[utcNow()]"
2134-
},
2135-
"utcShortValue": {
2136-
"type": "string",
2137-
"defaultValue": "[utcNow('d')]"
2138-
},
2139-
"utcCustomValue": {
2140-
"type": "string",
2141-
"defaultValue": "[utcNow('M d')]"
2142-
}
2143-
},
2144-
"resources": [
2145-
],
2146-
"outputs": {
2147-
"utcOutput": {
2148-
"type": "string",
2149-
"value": "[parameters('utcValue')]"
2150-
},
2151-
"utcShortOutput": {
2152-
"type": "string",
2153-
"value": "[parameters('utcShortValue')]"
2154-
},
2155-
"utcCustomOutput": {
2156-
"type": "string",
2157-
"value": "[parameters('utcCustomValue')]"
2158-
}
2159-
}
2160-
}
2161-
```
2162-
2163-
The output from the preceding example varies for each deployment but will be similar to:
2164-
2165-
| Name | Type | Value |
2166-
| ---- | ---- | ----- |
2167-
| utcOutput | string | 20190305T175318Z |
2168-
| utcShortOutput | string | 03/05/2019 |
2169-
| utcCustomOutput | string | 3 5 |
2170-
2171-
The next example shows how to use a value from the function when setting a tag value.
2172-
2173-
```json
2174-
{
2175-
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
2176-
"contentVersion": "1.0.0.0",
2177-
"parameters": {
2178-
"utcShort": {
2179-
"type": "string",
2180-
"defaultValue": "[utcNow('d')]"
2181-
},
2182-
"rgName": {
2183-
"type": "string"
2184-
}
2185-
},
2186-
"resources": [
2187-
{
2188-
"type": "Microsoft.Resources/resourceGroups",
2189-
"apiVersion": "2018-05-01",
2190-
"name": "[parameters('rgName')]",
2191-
"location": "westeurope",
2192-
"tags":{
2193-
"createdDate": "[parameters('utcShort')]"
2194-
},
2195-
"properties":{}
2196-
}
2197-
],
2198-
"outputs": {
2199-
"utcShort": {
2200-
"type": "string",
2201-
"value": "[parameters('utcShort')]"
2202-
}
2203-
}
2204-
}
2205-
```
2206-
22072097
## Next steps
22082098
* For a description of the sections in an Azure Resource Manager template, see [Authoring Azure Resource Manager templates](template-syntax.md).
22092099
* To merge multiple templates, see [Using linked templates with Azure Resource Manager](linked-templates.md).

articles/azure-resource-manager/templates/template-functions.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Template functions
33
description: Describes the functions to use in an Azure Resource Manager template to retrieve values, work with strings and numerics, and retrieve deployment information.
44
ms.topic: conceptual
5-
ms.date: 02/13/2020
5+
ms.date: 04/06/2020
66
---
77
# ARM template functions
88

@@ -72,6 +72,13 @@ Resource Manager provides several functions for making comparisons in your templ
7272
<a id="parameters" aria-hidden="true" />
7373
<a id="variables" aria-hidden="true" />
7474

75+
## Date functions
76+
77+
Resource Manager provides the following functions for working with dates.
78+
79+
* [dateTimeAdd](template-functions.date.md#datetimeadd)
80+
* [utcNow](template-functions-date.md#utcnow)
81+
7582
## Deployment value functions
7683

7784
Resource Manager provides the following functions for getting values from sections of the template and values related to the deployment:
@@ -218,7 +225,6 @@ Resource Manager provides the following functions for working with strings:
218225
* [uri](template-functions-string.md#uri)
219226
* [uriComponent](template-functions-string.md#uricomponent)
220227
* [uriComponentToString](template-functions-string.md#uricomponenttostring)
221-
* [utcNow](template-functions-string.md#utcnow)
222228

223229
## Next steps
224230

articles/azure-resource-manager/templates/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@
291291
href: template-functions-array.md
292292
- name: Comparison functions
293293
href: template-functions-comparison.md
294+
- name: Date functions
295+
href: template-functions-date.md
294296
- name: Deployment functions
295297
href: template-functions-deployment.md
296298
- name: Logical functions

0 commit comments

Comments
 (0)