Skip to main content

$toTimestamp

The $toTimestamp operator converts a date object to a timestamp.

Input

  • An array containing a date object and an optional granularity parameter (0 for seconds, 3 for milliseconds, 6 for microseconds, 9 for nanoseconds)

Output

  • The timestamp representation of the date object

Examples

Convert Date to Unix Timestamp (Seconds)

{
  "$toTimestamp": [
    {
      "$toDate": ["2023-01-15T10:30:00Z"]
    }
  ]
}
Result: 1673777400 (seconds since Unix epoch)

Convert Date to Milliseconds

{
  "$toTimestamp": [
    {
      "$toDate": ["2023-01-15T10:30:00Z"]
    },
    3
  ]
}
Result: 1673777400000 (milliseconds since Unix epoch)

Field Value

{
  "$toTimestamp": [
    {
      "$input": "date_value"
    },
    0
  ]
}
Result: The timestamp in seconds since Unix epoch for the date_value field