2012-08-17 10 views
5

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); 

Odpowiedz

5

Twoja pierwsza próba najpierw dodaje a potem odejmuje. Nie można jednak odjąć ciągów, dzięki czemu uzyskają one NaN.

Druga trota nie ma zamknięcia ). Poza tym, dzwonisz pod numer getTime. Musisz użyć new Date(...).getTime(). Zauważ, że otrzymujesz wynik w milisekundach podczas odejmowania dat. Możesz sformatować to przez wykupienie pełnych dni/godzin/itd.

+0

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))); –

+0

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

+0

Powracająca wartość to -604800000 Przyjmuję, że to znaczy, że minęło to dawno temu w sekundach? –

9

Dobra dla tych, którzy chcieliby przedstawić przykładowy przykład, jest prosty DateDiff ex, który mówi różnicę dat według dnia w wartości ujemnej (data już minęła) lub pozytywna (data się zbliża).

EDYCJA: Zaktualizowałem ten skrypt, aby wykonał pracę z nogami dla ciebie i przekonwertował wyniki w tym przypadku na -10, co oznacza, że ​​data minęła. Wprowadź własne daty currDate i iniPastedDate i powinieneś być dobry!

//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... 
+1

Zauważ, że jest to dość ograniczone - 'getDay' zwraca dzień tygodnia, więc jest w zasadzie możliwe do zastosowania tylko wtedy, gdy obie daty są w tym samym tygodniu (' iniremDate = "8/8/2012" 'dałoby taki sam wynik). – pimvdb

+0

@pimvdb co zrobić, jeśli używasz getTime, czy to działa na dowolny dzień? –

+1

'getTime' nie jest względny w stosunku do bieżącego tygodnia lub czegoś podobnego; daje niepowtarzalny numer dla każdej daty, więc jest niezawodny dla twoich potrzeb. Jedyny problem polega na tym, że sam musisz przekonwertować różnicę milisekund na różnicę dni. – pimvdb

2
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); 
0

Nie trzeba zawierać JQuery lub jakąkolwiek inną bibliotekę strony trzeciej.

Określ swój format daty wejściowej w tagu tytułu.

HTML:

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

Zastosowanie funkcji javascript:

IP_dateDiff (strDate1, strDate2, strDateFormat, debugowanie [prawda/fałsz])

alert (IP_dateDiff ('11 -12-2014' , '12.12.2014', 'DD-MM-rrrr' fałsz));

Funkcja IP_dateDiff zwraca liczbę dni.

Powiązane problemy