Last active
August 21, 2016 20:48
-
-
Save nitishn/da1a02669177a82eae3b12d7fbe014d4 to your computer and use it in GitHub Desktop.
I needed to find the current day of the year in javascript. Here's a snippet which helps you do that.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Taken from http://www.devcurry.com/2011/08/javascript-find-day-of-year.html | |
var timeStamp = new Date().setFullYear(new Date().getFullYear(), 0, 1); | |
var yearFirstDay = Math.floor(timeStamp / 86400000); | |
var today = Math.ceil((new Date().getTime()) / 86400000); | |
var dayOfYear = today - yearFirstDay; | |
// If you want hours, you can just multiply by 24. | |
// I subtracted by 24 because I wanted the beginning of the day, not the end! | |
var hoursOfYear = dayOfYear * 24 - 24 | |
// If you want the CURRENT hour, you need what the hour is now and simply add it to hoursOfYear | |
var currentHourInYear = dayOfYear + [what-ever-the-hour-is-now]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment