{"id":1768,"date":"2020-10-01T08:00:32","date_gmt":"2020-10-01T15:00:32","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=1768"},"modified":"2020-09-30T09:29:18","modified_gmt":"2020-09-30T16:29:18","slug":"new-date-and-time-system-functions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/new-date-and-time-system-functions\/","title":{"rendered":"New date and time system functions in Azure Cosmos DB"},"content":{"rendered":"<p>We are excited to announce support for the following system functions that help developers convert date and time values:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimeadd\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimeAdd<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimediff\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimeDiff<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimefromparts\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimeFromParts<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimepart\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimePart<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimetoticks\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimeToTicks<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimetotimestamp\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimeToTimestamp<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-tickstodatetime\" target=\"_blank\" rel=\"noopener noreferrer\">TicksToDateTime<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-timestamptodatetime\" target=\"_blank\" rel=\"noopener noreferrer\">TimestampToDateTime<\/a><\/li>\n<\/ul>\n<p>These new system functions are available in all current SQL (core) API SDK\u2019s. For the .NET SDK, these system functions are available in <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Azure.Cosmos\/3.13.0\">version 3.13<\/a> or later. Learn about <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-date-time-functions\" target=\"_blank\" rel=\"noopener noreferrer\">existing date and time system functions<\/a>.<\/p>\n<p>Let\u2019s check out some examples for working with dates and times in Azure Cosmos DB, including some examples that use these new system functions. As a reminder, Azure Cosmos DB stores DateTime values as regular strings. There is no special storage format for DateTime values.<\/p>\n<p>The recommended format for DateTime strings in Azure Cosmos DB is\u00a0<strong>yyyy-MM-ddTHH:mm:ss.fffffffZ<\/strong>\u00a0which follows the <a href=\"https:\/\/en.wikipedia.org\/wiki\/ISO_8601\" target=\"_blank\" rel=\"noopener noreferrer\">ISO 8601 UTC standard<\/a>. You could, alternatively, use an epoch value to represent time as a number, in either <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-getcurrenttimestamp\" target=\"_blank\" rel=\"noopener noreferrer\">milliseconds<\/a> or <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-getcurrentticks\" target=\"_blank\" rel=\"noopener noreferrer\">100-nanosecond ticks<\/a>. \u00a0Comparing different date and time values, requires a consistent format. For example, you cannot compare an ISO 8601 UTC standard DateTime string to an epoch value without the appropriate <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimetotimestamp\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimeToTimestamp<\/a> or <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimetoticks\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimeToTicks<\/a> conversion.<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-size: 18pt;\">Comparing DateTime strings<\/span><\/h2>\n<p>Both equality and range comparisons between DateTime values in the format <strong>yyyy-MM-ddTHH:mm:ss.fffffffZ<\/strong>\u00a0 are supported and will utilize the index.<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT (\"2020-05-05T19:21:27.0000000Z\" &gt; \"2020-09-05T00:00:0.1234567Z\") AS ComparisonResult<\/pre>\n<p><strong>Result:<\/strong><\/p>\n<pre>[\r\n \u00a0\u00a0 {\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"ComparisonResult\": false\r\n \u00a0\u00a0 }\r\n]<\/pre>\n<p>Since DateTime strings are treated as ordinary strings by the query engine, DateTime strings must have a consistent precision for valid equality comparisons.<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-size: 18pt;\">Identifying the current month based on a DateTime strings<\/span><\/h2>\n<p>You can use the new <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/sql-query-datetimepart\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimePart<\/a> system function to calculate the current month (or other unit of time) based on a DateTime string.<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT DateTimePart(\"m\", \"2020-01-02T03:04:05.6789123Z\") AS MonthValue<\/pre>\n<p><strong>Result:<\/strong><\/p>\n<pre>[\r\n \u00a0\u00a0 {\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"MonthValue\": 1\r\n \u00a0\u00a0 }\r\n]<\/pre>\n<h2><span style=\"font-size: 18pt;\">Constructing a DateTime from parts<\/span><\/h2>\n<p>If you have the individual components, you can now construct a DateTime string on the fly.<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT DateTimeFromParts(2020, 9, 4, 10, 52, 12, 3456789) AS DateTime<\/pre>\n<p><strong>Result:<\/strong><\/p>\n<pre>[\r\n \u00a0\u00a0 {\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"DateTime\": \"2020-09-04T10:52:12.3456789Z\"\r\n \u00a0\u00a0 }\r\n]<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<h2><span style=\"font-size: 18pt;\">Converting the system _ts property to a DateTime string<\/span><\/h2>\n<p>Items in Cosmos DB have a _ts system property. The _ts value is an epoch value in seconds (not milliseconds) since an item was last modified. To convert the _ts value from seconds to milliseconds or 100-nanosecond ticks, simply multiply by 1,000 or 10,000,000, respectively. You can then convert this value to a DateTime.<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT TOP 1 TimestampToDateTime(c._ts*1000) AS DateTime\r\nFROM c<\/pre>\n<pre><strong>Result:<\/strong>\r\n[\r\n    {\r\n        \"DateTime\": \"2019-08-30T18:49:11.0000000Z\"\r\n    }\r\n]<\/pre>\n<h2><span style=\"font-size: 18pt;\">Converting to local time zone<\/span><\/h2>\n<p>If you need to convert a DateTime value from UTC time to a local time zone, you can do the conversion using the <a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-datetimeadd\" target=\"_blank\" rel=\"noopener noreferrer\">DateTimeAdd<\/a> system function.<\/p>\n<p>Here\u2019s an example that converts a UTC time to India Standard Time (UTC+5:30)<\/p>\n<p><strong>Query:<\/strong><\/p>\n<pre>SELECT DateTimeAdd(\"minute\", 330, \"2020-01-01T00:00:00.1234567Z\") AS IndiaStandardTime<\/pre>\n<p><strong>Result:<\/strong><\/p>\n<pre>[\r\n \u00a0\u00a0 {\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"IndiaStandardTime\": \"2020-01-01T05:30:00.1234567Z\"\r\n \u00a0\u00a0 }\r\n]\r\n\r\n<\/pre>\n<p><strong>Learn more:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/working-with-dates\" target=\"_blank\" rel=\"noopener noreferrer\">Working with dates in Azure Cosmos DB<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/sql-query-date-time-functions\" target=\"_blank\" rel=\"noopener noreferrer\">Date and time system functions<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learn about new system functions for working with dates and times in Azure Cosmos DB<\/p>\n","protected":false},"author":12128,"featured_media":61,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[12,14,818,19],"tags":[1752],"class_list":["post-1768","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcements","category-core-sql-api","category-query","category-tips-and-tricks","tag-datetime"],"acf":[],"blog_post_summary":"<p>Learn about new system functions for working with dates and times in Azure Cosmos DB<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/1768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/12128"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=1768"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/1768\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/61"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=1768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=1768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=1768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}