Skip to content

Instantly share code, notes, and snippets.

@DewofyourYouth
Created November 6, 2018 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DewofyourYouth/7aee59ab72b8497f70a957db8293b0d8 to your computer and use it in GitHub Desktop.
Save DewofyourYouth/7aee59ab72b8497f70a957db8293b0d8 to your computer and use it in GitHub Desktop.
Some quick code snippets for tutorial on working with the JavaScript date object.
//iterate date
var myDate = new Date();
var dateInMS = myDate.getTime();
// day in milliseconds = 1000 (milliseconds in a second) * 60 (seconds in a minute) * 60 (minutes in an hour) * 24 (hours in a day) = 86400000 (millseconds in a day)
// the getTime() method gets the time in milliseconds since January 1, 1970.
var tomorrow = new Date(datInMS + 86400000);
console.log(myDate);
console.log(tomorrow);
// VM1911:3 Tue Nov 06 2018 14:35:25 GMT+0200 (Israel Standard Time)
// VM1911:4 Wed Nov 07 2018 14:35:25 GMT+0200 (Israel Standard Time)
//Elapsed time:
var start = new Date();
doSomething();
var end = new Date();
var elapsed = end.getTime() - start.getTime();
console.log(elapsed);
function doSomething() {
for(var i = 0; i < 100000; i++){
}
}
//Iterate over a weeks worth of dates:
var now = new Date();
var newDate = now;
for(var i = 0; i < 7; i++){
console.log(newDate.toUTCString());
newDate = new Date(newDate.getTime() + 86400000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment