Skip to content

Commit 44df461

Browse files
authored
DOCSP-32377 ObjectId with Custom Date (#7059)
* DOCSP-32377 ObjectId with Custom Date * * * * * JA feedback
1 parent 721aa32 commit 44df461

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

source/reference/method/ObjectId.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,49 @@ The method returns:
121121

122122
507f191e810c19729de860ea
123123

124+
Specify a Date
125+
~~~~~~~~~~~~~~
126+
127+
You can use a custom :ref:`document-bson-type-date` to specify an ObjectId.
128+
129+
.. procedure::
130+
:style: normal
131+
132+
.. step:: Set a variable for your specified date
133+
134+
Internally, Date objects are stored as signed
135+
64-bit integer that represents the number of milliseconds since the
136+
:wikipedia:`Unix epoch`. To learn more, see :method:`Date()`.
137+
138+
.. code-block:: javascript
139+
:copyable: true
140+
141+
myDate = new Date( "2024-01-01" )
142+
143+
.. step:: Convert your Date object to seconds
144+
145+
.. code-block:: javascript
146+
:copyable: true
147+
148+
timestamp = Math.floor( myDate / 1000 )
149+
150+
.. step:: Set your new ObjectId with ``timestamp`` as the argument
151+
152+
You can verify the Date by using :method:`ObjectId.getTimestamp()`.
153+
154+
.. io-code-block::
155+
:copyable: true
156+
157+
.. input::
158+
:language: javascript
159+
160+
newObjectId = ObjectId(timestamp)
161+
162+
.. output::
163+
:language: javascript
164+
165+
ObjectId("6592008029c8c3e4dc76256c")
166+
124167
Specify an Integer String
125168
~~~~~~~~~~~~~~~~~~~~~~~~~
126169

0 commit comments

Comments
 (0)