Mam problem z funkcją DateDiff. Próbuję znaleźć różnicę między dwiema datami/czasami. Przeczytałem to ogłoszenie (What's the best way to calculate date difference in Javascript) i również obejrzałem ten samouczek (http://www.javascriptkit.com/javatutors/datedifference.shtml), ale nie mogę go uzyskać.JavaScript DateDiff
Oto, co próbowałem dostać do pracy bez powodzenia. Czy ktoś mógłby mi powiedzieć, co robię i jak mogę to uprościć. Wydaje się trochę za dużo ...?
//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);
Jak pan powiedział, pokazałoby to wartość ujemną. Czy to jest poprawne, wydaje się poprawne, ponieważ pokazuje wartości ujemne. \t \t document.write (nowa data (iniremDate) -new Data (currDate)); \t \t \t \t funkcja DateDiff (date1, data2) { \t \t powrotu date1.getTime() - date2.getTime(); \t \t} \t \t \t \t \t document.write (DateDiff (New Data (iniremDate), nowe data (currDate))); –
Ponieważ 'iniremDate' jest 10 sierpnia, a teraz jest 17-ty, otrzymujesz oczywiście wartość ujemną. Jeśli chcesz odjąć przeciwieństwo, po prostu odejmij 'date1' od' date2'. – pimvdb
Powracająca wartość to -604800000 Przyjmuję, że to znaczy, że minęło to dawno temu w sekundach? –