Skip to content

Instantly share code, notes, and snippets.

@kumarharsh
Created May 19, 2013 15:40
Show Gist options
  • Save kumarharsh/5608030 to your computer and use it in GitHub Desktop.
Save kumarharsh/5608030 to your computer and use it in GitHub Desktop.
A snippet to convert Mongo ObjectID to javascript timestamps, and vice-versa
----- mongo to js ------
timeStamp = parseInt(Mongo.ObjectId().toString().substr(0,8), 16)*1000
date = new Date(timestamp)
-----------------------------
----- js to mongo -----
timestamp = new Date().getTime()/1000
mongoId = new ObjectID(timestamp.toString(16)+1e16)
@z347
Copy link

z347 commented Jan 5, 2022

Thank you, man :)

@bcdave146
Copy link

Thanks man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment