Mengambil nilai jarak antara dua tanggal (javascript)
Menampilkan jarak jumlah hari diantara 2 tanggal.
contoh:
tanggal mulai: 2009-01-29
tanggal akhir: 2009-02-03
hasil jarak tanggal dalam jumlah hari = 5
Fungsi scriptnya sbb, simpan dengan nama file datediff.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
var DayName=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; var oneMinute=1000*60; var intervalObject=new Object(); intervalObject["yyyy"]={units:1000*60*60*24*365,measure:"year"}; intervalObject["m"]={units:1000*60*60*24*30,measure:"month"}; intervalObject["d"]={units:1000*60*60*24,measure:"day"}; intervalObject["Q"]={units:intervalObject["m"].units*3,measure:"quarter"}; intervalObject["H"]={units:oneMinute*60,measure:"hour"}; intervalObject["N"]={units:oneMinute,measure:"minute"}; intervalObject["S"]={units:1000,measure:"second"}; function DateDiff(dateAddObj){ this.interval=dateAddObj.interval; this.date1=dateAddObj.date1; this.date2=dateAddObj.date2; this.calculate=calculate; this.calculate(); } Date.prototype.DateDiff=DateDiff; function calculate(){ var paramDate1=new String(this.date1); splitDate1=paramDate1.split("-"); paramDateYear1=splitDate1[0]; paramDateMonth1=splitDate1[1]-1; paramDateDay1=splitDate1[2]; if(paramDateMonth1>12){ alert("Invalid Month!"); return false; } if(paramDateDay1>31){ alert("Invalid Day!"); return false; } var paramDate2=new String(this.date2); splitDate2=paramDate2.split("-"); paramDateYear2=splitDate2[0]; paramDateMonth2=splitDate2[1]-1; paramDateDay2=splitDate2[2]; if(paramDateMonth2>12){ alert("Invalid Month!"); return false; } if(paramDateDay2>31){ alert("Invalid Day!"); return false; } var paramDate1Object=new Date(paramDateYear1,paramDateMonth1,paramDateDay1); paramDate1Object.setHours(0); paramDate1Object.setMinutes(0); paramDate1Object.setSeconds(0); //paramDate1Object.getTimezoneOffset() * oneMinute; var paramDate1ObjectTime=paramDate1Object.getTime(); var paramDate2Object=new Date(paramDateYear2,paramDateMonth2,paramDateDay2); paramDate2Object.setHours(0); paramDate2Object.setMinutes(0); paramDate2Object.setSeconds(0); var paramDate2ObjectTime=paramDate2Object.getTime(); if(paramDate2Object>paramDate1Object){ DSTAdjust=(paramDate2Object.getTimezoneOffset() - paramDate1Object.getTimezoneOffset()) * oneMinute; } else{ DSTAdjust=(paramDate1Object.getTimezoneOffset() - paramDate2Object.getTimezoneOffset()) * oneMinute; } if(typeof intervalObject[this.interval]!="undefined"){ if(typeof intervalObject[this.interval].units=="undefined"){ alert("Interval is invalid!"); return false; } // var diff=Math.abs(paramDate2ObjectTime-paramDate1ObjectTime) - DSTAdjust; // yg ini saya ubah tidak memakai abs, supaya hasil minus tetap minus, berguna untuk validasi dll.. var diff=(paramDate2ObjectTime-paramDate1ObjectTime) - DSTAdjust; var timeDiff=Math.floor(diff/intervalObject[this.interval].units); if(timeDiff>1){ var rname=intervalObject[this.interval].measure + "s"; } else{ var rname=intervalObject[this.interval].measure; } this.difference=parseInt(timeDiff); } else{ this.difference="Wrong format of interval!"; } } |
Penggunaan:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<html> <head> <script type="text/javascript" src="datediff.js"></script> </head> <body> <script type="text/javascript"> var curDate=new Date(); startDate="2009-02-02"; endDate="2009-01-30"; curDate.DateDiff({interval:"d",date1:startDate,date2:endDate}); alert(curDate.difference); </script> </body> |
Maka hasilnya akan ada alert berjumlah: -3
Hasil minus disini bisa berguna untuk cek validasi dan lain2.. sesuaikan dgn kebutuhan aplikasi kita.
Sumber Core JS: http://www.softxml.com/jsdatefunctions/datefunctions.htm
Beberapa code telah dimodif, silahkan lihat komentar pada file script “datediff.js”
Semoga bermanfaat
terimakasih mas sangat membantu 🙂
“we gy pain nich…….?
“leh gbung g’……..?
@eva, boleh2 saja..
kalo hitung umur pake function itu gimana??
thx be4
@dian,
coba script ini: