JavaScript calculate the day of the year (1 - 366)
Following OP's edit:
var now = new Date();var start = new Date(now.getFullYear(), 0, 0);var diff = now - start;var oneDay = 1000 * 60 * 60 * 24;var day = Math.floor(diff / oneDay);console.log('Day of year: ' + day);
Get full date from day of the year
The shortest way:
new Date(2016, 0, 208)
will return Date 2016-07-26
How to get the date from x-th day of the year?
Here is a function if you pass the year and the nth day of that year this will return the date of that nth day.
function dateFromDay(year, day){ var date = new Date(year, 0); return new Date(date.setDate(day));}console.log(dateFromDay(2010, 301));console.log(dateFromDay(2010, 365));
What is the best way to determine the number of days in a month with JavaScript?
function daysInMonth (month, year) { // Use 1 for January, 2 for February, etc.
return new Date(year, month, 0).getDate();
}
console.log(daysInMonth(2, 1999)); // February in a non-leap year.
console.log(daysInMonth(2, 2000)); // February in a leap year.
Calculate total number of days in a year
You can find the number of days using the simple leap year algorithem:
In the Gregorian calendar three criteria must be taken into account to
identify leap years: The year can be evenly divided by 4; If the year
can be evenly divided by 100, it is NOT a leap year, unless; The year
is also evenly divisible by 400. Then it is a leap year.
function daysInYear(year) { return ((year % 4 === 0 && year % 100 > 0) || year %400 == 0) ? 366 : 365;}
console.log(daysInYear(2016));
console.log(daysInYear(2017));
How can I calculate the number of years between two dates?
Probably not the answer you're looking for, but at 2.6kb, I would not try to reinvent the wheel and I'd use something like moment.js. Does not have any dependencies.
The diff
method is probably what you want: http://momentjs.com/docs/#/displaying/difference/
is there any simplest way to calculate a recurring day?
These are powers of two, meant to be used in a bit field.
You are meant to tell whether a given date is "checked" using bitwise operator &
.
const SUNDAY = 64; // 0100 0000
const SATURDAY = 32; // 0010 0000
const FRIDAY = 16; // 0001 0000
const THURSDAY = 8; // 0000 1000
// etc.
let day = 48; // 0011 0000
day & SUNDAY // 0 indicating Sunday should not be checked
day & SATURDAY // 32 indicating Saturday should be checked
day & FRIDAY // 16 indicated Friday should be checked
day & THURSDAY // 0 indicated Thursday should not be checked
If you want to turn this into an array of checked values, the simplest option would be to stop using a bitfield an instead store a JSON array of strings.
Assuming that's not possible, your case statement can be replaced with something as simple as...
const DAYS = [ 1, 2, 4, 8, 16, 32, 64 ];
const dayBitfield = 41;
DAYS.filter(day => dayBitfield & day) // [1, 8, 32]
how can I convert day of year to date in javascript?
"I want to take a day of the year and convert to an actual date using the Date object."
After re-reading your question, it sounds like you have a year number, and an arbitrary day number (e.g. a number within 0..365
(or 366 for a leap year)), and you want to get a date from that.
For example:
dateFromDay(2010, 301); // "Thu Oct 28 2010", today ;)
dateFromDay(2010, 365); // "Fri Dec 31 2010"
If it's that, can be done easily:
function dateFromDay(year, day){
var date = new Date(year, 0); // initialize a date in `year-01-01`
return new Date(date.setDate(day)); // add the number of days
}
You could add also some validation, to ensure that the day number is withing the range of days in the year supplied.
Related Topics
What Is This JavaScript Pattern Called and Why Is It Used
How to Link Each User to Their Data in Firebase
When and Why to 'Return False' in JavaScript
Does Never Resolved Promise Cause Memory Leak
JavaScript Url Decode Function
How to Prevent Caching of My JavaScript File
JSON.Stringify() Array Bizarreness with Prototype.Js
Programmatically Open New Pages on Tabs
Window.Onbeforeunload Ajax Request in Chrome
(1, Eval)('This') VS Eval('This') in JavaScript
Keyboard Shortcuts with Jquery
How Does One Capture a MAC's Command Key via JavaScript
How to Access a Numeric Property
Firebase: Setting Additional User Properties