Skip to content

Commit 4d03db4

Browse files
authored
Merge pull request #216153 from seesharprun/cosmos-timestamp-example
Cosmos DB | Update timestamp query article
2 parents 36a8a4a + 900b21b commit 4d03db4

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

articles/cosmos-db/nosql/query/timestamptodatetime.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ author: seesharprun
55
ms.service: cosmos-db
66
ms.subservice: nosql
77
ms.topic: conceptual
8-
ms.date: 08/18/2020
8+
ms.date: 10/27/2022
99
ms.author: sidandrews
1010
ms.reviewer: jucocchi
1111
ms.custom: query-reference, ignite-2022
1212
---
13+
1314
# TimestampToDateTime (Azure Cosmos DB)
15+
1416
[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)]
1517

1618
Converts the specified timestamp value to a DateTime.
@@ -23,7 +25,7 @@ TimestampToDateTime (<Timestamp>)
2325

2426
## Arguments
2527

26-
*Timestamp*
28+
### Timestamp
2729

2830
A signed numeric value, the current number of milliseconds that have elapsed since the Unix epoch. In other words, the number of milliseconds that have elapsed since 00:00:00 Thursday, 1 January 1970.
2931

@@ -43,27 +45,58 @@ Returns the UTC date and time ISO 8601 string value in the format `YYYY-MM-DDThh
4345
|.fffffff|seven-digit fractional seconds|
4446
|Z|UTC (Coordinated Universal Time) designator|
4547

46-
For more information on the ISO 8601 format, see [ISO_8601](https://en.wikipedia.org/wiki/ISO_8601)
48+
For more information on the ISO 8601 format, see [ISO_8601](https://en.wikipedia.org/wiki/ISO_8601)
4749

4850
## Remarks
4951

5052
TimestampToDateTime will return `undefined` if the timestamp value specified is invalid.
5153

5254
## Examples
5355

54-
The following example converts the timestamp to a DateTime:
56+
The following example converts the value `1,594,227,912,345` from milliseconds to a date and time of **July 8, 2020, 5:05 PM UTC**.
5557

5658
```sql
5759
SELECT TimestampToDateTime(1594227912345) AS DateTime
5860
```
5961

6062
```json
6163
[
62-
{
63-
"DateTime": "2020-07-08T17:05:12.3450000Z"
64-
}
64+
{
65+
"DateTime": "2020-07-08T17:05:12.3450000Z"
66+
}
67+
]
68+
```
69+
70+
This next example uses the timestamp from an existing item in a container. The item's timestamp is expressed in seconds.
71+
72+
```json
73+
{
74+
"id": "8cc56bd4-5b8d-450b-a576-449836171398",
75+
"type": "reading",
76+
"data": "temperature",
77+
"value": 35.726545156,
78+
"_ts": 1605862991
79+
}
80+
```
81+
82+
To use the `_ts` value, you must multiply the value by 1,000 since the timestamp is expressed in seconds.
83+
84+
```sql
85+
SELECT
86+
TimestampToDateTime(r._ts * 1000) AS timestamp,
87+
r.id
88+
FROM
89+
readings r
90+
```
91+
92+
```json
93+
[
94+
{
95+
"timestamp": "2020-11-20T09:03:11.0000000Z",
96+
"id": "8cc56bd4-5b8d-450b-a576-449836171398"
97+
}
6598
]
66-
```
99+
```
67100

68101
## Next steps
69102

0 commit comments

Comments
 (0)