0

For my website I need to calculate the number of days between the current date and the previous (so not next) socalled billingdate (a fixed annual date (e.g., fixed day and month) on which the bill is sent to the customer) in javascript. The billingdate retrieved from the database is a string, of which year value is not relevant. Also note that in the example below the month-day of the billing date is before the month-day of the current date, but it's also possible that its after the current date

So I have:

var nrofdays; //since last billing date

var currentdate = new Date();

var billingdate = '2010-06-23'; //year value is is not relevant

I'm familiar with the methods of Date, but I still struggle to code this in an elegant way. Any suggestions?

Joppo
  • 715
  • 2
  • 12
  • 31
  • You can do `new Date(string)` too. Check here for more info https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – elclanrs Dec 20 '13 at 09:14

4 Answers4

0
Math.floor((Date.now() - new Date("2010-6-23")) / (1000 * 60 * 60 * 24))

Be careful though, Javascript months range from 0 to 11. So month 6 is July.

Tarek Ayna
  • 340
  • 4
  • 10
0
var currentdate = new Date();

var billingdate = "2010-06-23";    

function delta(date1, date2) {
    return (date2 - date1)/(1000*60*60*24)
}
function parseDate(input) {
  var parts = input.split('-');
  return new Date(parts[0], parts[1]-1, parts[2]);
}

var days = Math.floor( delta(parseDate(billingdate), currentdate )  );
alert( days );
Rami.Q
  • 2,486
  • 2
  • 19
  • 30
0

You may like to try below

function parseBillDate(dateString) {
    var dateStringArr = dateString.split('-');
    var currentDate = new Date();
    return new Date(currentDate.getFullYear(), dateStringArr[1] - 1, dateStringArr[2]);
}

function dayDiffInDays(dateOne, dateTwo) {
    return (dateOne-dateTwo)/(1000*60*60*24);
}

var billDate = '2010-06-23';
var diffInDays = dayDiffInDays(parseBillDate(billDate), new Date());

parseBillDate: Parses the bill date and sets the year to current year

dayDiffInDays: Converts difference from milliseconds to days

Sajad Deyargaroo
  • 1,149
  • 1
  • 7
  • 20
  • Please don't use hungarian notation.http://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation – Björn Roberg Dec 20 '13 at 09:46
  • @Bjorn: didnt heard about Hungarian notation till now. Sajad's code does work however when I tried it in JSFiddle...What kind of risk does Hungaraion notation create? Any suggestions for better code/improvement of Sajad's code? – Joppo Dec 20 '13 at 09:55
  • Just read some more about the hungarian string notation (http://en.wikipedia.org/wiki/Hungarian_notation). I see no further problems with the code if the notation would be changed - agree?. @Sajad: if youre willing to perform the changes I'll accept this as the working solution. – Joppo Dec 20 '13 at 10:13
  • I have updated the variable names. Actually, my focus was on the algorithm and not on the names. I just tried to save some characters while naming the variables. Anyway, it is done now. – Sajad Deyargaroo Dec 20 '13 at 14:22
-1

You can set hours to 0 to make only the days relevant. And set the year to any year as long as they're the same it will work. You will have to do some logic to figure out which date is earlier.

function rangeDates(d1, d2) {

  var oneDay = 24*3600*1000;
  var dayCount = 0,
     ms = d1*1;
  for (last=d2*1; ms < last; ms+=oneDay) {
    dayCount++;
  }
  return dayCount;

}

var d2 = new Date(),
    d1 = new Date('2010-06-23');

    d2.setHours(0,0,0,0);
    d1.setHours(0,0,0,0);
    d2.setFullYear(1995);
    d1.setFullYear(1995); // party like its 1995

var dates = rangeDates(d1,d2);
console.log(dates);

Fiddle

el_pup_le
  • 11,711
  • 26
  • 85
  • 142