File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -121,6 +121,49 @@ The method returns:
121
121
122
122
507f191e810c19729de860ea
123
123
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
+
124
167
Specify an Integer String
125
168
~~~~~~~~~~~~~~~~~~~~~~~~~
126
169
You can’t perform that action at this time.
0 commit comments