Cosmos DB automactially adds a timestamp field called “_ts”. everytime a document is created or updated. And it looks like this…

{
  "_ts": 1628083065
}

That number is described by this blog post as an “epoch value in seconds (not milliseconds) since an item was last modified.” The number of seconds since what? Well, the number of seconds since January 1st, 1970 to be exact. This is a Unix date format (aslo called “epoch”) and you can use JavaScript to convert that into a date object that you can actually use.

Converting JavaScript Date

You can create a JavaScript Date object from an epoch value by just passing the value to the Date constructor. HOWEVER; the docs for the JavaScript Date object say that “Date objects contain a number that respresents the number of milliseconds since January 1, 1970.” By contrast, the Cosmos DB _ts property is the number of seconds since January 1, 1970. So in order to get the correct time, we have to multiply the _ts value by 1000 so that it’s milliseconds instead of seconds. So the final answer looks like this…

const d = new Date(_ts * 1000);
console.log(d.toDateString());

// OUPTUT: 'Wed Aug 04 2021'