Skip to content

Instantly share code, notes, and snippets.

@realguess
Created September 11, 2013 19:39
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 realguess/6528734 to your computer and use it in GitHub Desktop.
Save realguess/6528734 to your computer and use it in GitHub Desktop.
Get the number of days since the beginning of the year.
// Days Passed Since New Year
// ==========================
//
// (c) 2013 Chao Huang <chao@realguess.net>
// Quick Example
// -------------
function days() {
return (new Date(new Date().toISOString().substr(0,10)).getTime() - new Date('' + new Date().getFullYear()).getTime()) / (1000 * (60 * 60 * 24));
}
console.log(days());
// Detail Explanation
// ------------------
(function () {
var seconds = 60 * 60 * 24; // seconds in a day
var day1 = new Date('' + new Date().getFullYear());
var today = new Date(new Date().toISOString().substr(0,10));
console.log((today.getTime() - day1.getTime()) / (1000 * seconds));
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment