Javascript DateDiff

I have a problem with DateDiff function. I am trying to figure out the difference between two dates / times. I read this post ( What is the best way to calculate the date difference in Javascript ) and I also reviewed this tutorial ( http://www.javascriptkit.com/javatutors/datedifference.shtml ), but I can not get it.

This is what I tried to achieve without success. Can someone please tell me what I am doing and how I can simplify this. Seems a bit coded ...?

//Set the two dates
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var currDate = month + "/" + day + "/" + year;
var iniremDate = "8/10/2012";

//Show the dates subtracted
document.write('DateDiff is: ' + currDate - iniremDate);

//Try this function...
function DateDiff(date1, date2) {
    return date1.getTime() - date2.getTime();
}

//Print the results of DateDiff
document.write (DateDiff(iniremDate, currDate);
+5
source share
4 answers

Your first attempt first adds and then subtracts. You still cannot deduct lines to get NaN.

). , getTime . new Date(...).getTime(). , . , // ..

+5

, , , - DateDiff ex, date diff ( ) ( ).

EDIT: script, , a -10, , . currDate iniPastedDate, , !

//Set the two dates
var currentTime   = new Date()
var currDate      = currentTime.getMonth() + 1 + "/" + currentTime.getDate() + "/" + currentTime.getFullYear() //Todays Date - implement your own date here.
var iniPastedDate = "8/7/2012" //PassedDate - Implement your own date here.

//currDate = 8/17/12 and iniPastedDate = 8/7/12

function DateDiff(date1, date2) {
    var datediff = date1.getTime() - date2.getTime(); //store the getTime diff - or +
    return (datediff / (24*60*60*1000)); //Convert values to -/+ days and return value      
}

//Write out the returning value should be using this example equal -10 which means 
//it has passed by ten days. If its positive the date is coming +10.    
document.write (DateDiff(new Date(iniPastedDate),new Date(currDate))); //Print the results...
+9
function setDateWeek(setDay){
    var d = new Date();
    d.setDate(d.getDate() - setDay); // <-- add this
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1;
    var curr_year = d.getFullYear();
    return curr_date + "-" + curr_month + "-" + curr_year;
}


setDateWeek(1);
+2

JQuery .

title.

HTML:

<script type = "text / javascript" src = "http://services.iperfect.net/js/IP_generalLib.js">

Use javascript function:

IP_dateDiff (strDate1, strDate2, strDateFormat, debug [true / false])

alerts (IP_dateDiff ('11 -12-2014 ', '12 -12-2014', 'DD-MM-YYYY', false));

The IP_dateDiff function will return the number of days.

0
source

All Articles