4

I'm looking for the equivalent of MySQL now(). Any format is fine.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Michael Munsey
  • 3,740
  • 1
  • 25
  • 15

2 Answers2

4

You should create a User Defined Function (UDF) like this.

function now() {
    return new Date();
}

You would call it like this:

SELECT udf.now(), c.date, c.id FROM c WHERE c.date < udf.now()
Larry Maccherone
  • 9,393
  • 3
  • 27
  • 43
  • 2
    your answer worked perfectly. Maybe you should explain the UDF in a little bit more details. I am very new to Azure Cosmos and did not get the first sentence at once. "You should create an User Defined Function like this" Was more appropriate for a begniner like me. Just a hint ... – achahbar Jun 24 '19 at 08:01
3

As of the end of 2020, there are 2 functions to get current date/time in Cosmos DB:

SELECT 
    GetCurrentDateTime() AS utc_datetime_iso_8601,
    GetCurrentTimestamp() AS timestamp_with_milliseconds

returns

[
    {
        "utc_datetime_iso_8601": "2020-12-25T22:56:27.3453234Z",
        "timestamp_with_milliseconds": 1608936987345
    }
]

docs

Max Ivanov
  • 5,695
  • 38
  • 52