Click here to Skip to main content
15,662,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
for example: Mai 6 (Without 2017) to Nov 7 (Without 2017) DAY:1, DAY:2, DAY:3..... And Nov 8 (Without 2017) to Mai 5 Next year (Without 2018) DAY:1, DAY:2, DAY:3..... I Have thish code but calculating the days only for this year and not from November 8 to next year Mai 5

What I have tried:

function getDays(strDay1, strDay2){ var day=1000*60*60*24;

strDay1 = strDay1 + ' ' + (new Date().getFullYear()); strDay2 = strDay2 + ' ' + (new Date().getFullYear());

var d1 = Date.parse(strDay1); var d2 = Date.parse(strDay2);

var diff = d2 - d1; return Math.round(diff/day) }

alert(getDays('Nov 8', 'Mai 5'));
Posted
Updated 14-Feb-19 0:29am

1 solution

Use The Following Function in Javascript.

function getDays(strDay1, strDay2) {
debugger;
var strMonth1 = strDay1.split(' ')[0];
var strMonth2 = strDay2.split(' ')[0];
var months = { JAN: 1, FEB: 2, MAR: 2, APR: 4, MAY: 5, JUN: 6, JUL: 7, AUG: 8, SEP: 9, OCT: 10, NOV: 11, DEC: 12 };
Month1 = months[strMonth1.toUpperCase()];
Month2 = months[strMonth2.toUpperCase()];
if (Month1 > Month2)
{
strDay1 = strDay1 + ' ' + (new Date().getFullYear());
strDay2 = strDay2 + ' ' + (new Date().getFullYear()+1);
}
else
{
strDay1 = strDay1 + ' ' + (new Date().getFullYear());
strDay2 = strDay2 + ' ' + (new Date().getFullYear());
}
var day = 1000 * 60 * 60 * 24;

var d1 = Date.parse(strDay1); var d2 = Date.parse(strDay2);

var diff = d2 - d1; return Math.round(diff / day)
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900