|
![](/images/resource/spacer.gif) |
|
JavaScript Different Date สวัสดีครับ คือว่าผมมี var Date อยู่ 2 ตัว อยากจะเอามาเปรียบเทียบว่า Date 2 ตัว |
|
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
Code
<html>
<head><title>การเปรียบเทียบวันที่และเวลา</title></head>
<body>
<script language="javascript">
function getTimeBetween(from, until) {
var past = from== " " ? new Date() : new Date(from) ;
var future = until == " " ? new Date() : new Date(until);
if(past>= future) {
var tmp = past ;
past = future ;
future = tmp ;
}
var between = [ future.getFullYear() - past.getFullYear(),
future.getMonth() - past.getMonth(),
future.getDate() - past.getDate(),
future.getHours() - past.getHours(),
future.getMinutes() - past.getMinutes(),
future.getSeconds() - past.getSeconds()];
if (between[2] < 0){
between[1] -- ;
var ynum = future.getFullYear();
var mlengths =[31, (ynum %4 == 0 && ynum%100 != 0 || ynum%400 ==0) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] ;
var mnum = future.getMonth() - 1;
if(mnum < 0) { mnum += 12; }
between[2] += mlengths[mnum] ;
}
if (between[1] < 0){
between[0]--;
between[1] += 12;
}
if (between[5]< 0){
between[4]--;
between[5] += 60;
}
if (between[4]< 0){
between[3]--;
between[4] += 60;
}
if (between[3]< 0){
between[2]--;
between[3] += 24;
}
return between ;
}
13/07/2009
var until = getTimeBetween("13 Jul, 2009", "sep 02, 2009") ; //เปลี่ยนวันที่ตรงนี้
alert(until[0] + " years " + until[1] + " months "+ until[2] + " days " + until[3]+" hours " + until[4] + " minutes " + until[5] + " seconds");
</script>
</body>
</html>
![time diff time diff](https://www.thaicreate.com/upload/stock/20090714104145.jpg?v=1001)
Thank & Ref : ktpbook
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2009-07-14 10:45:25 |
By :
panyapol |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
javascript datediff ถามพี่กูเค้าก็เจอครับ ถ้ายังหาไม่ได้ เดี๋ยวค้นในเครื่องให้
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2009-07-14 10:46:07 |
By :
plakrim |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
งะไม่ทันน้องแป๊ะ เร็วจริงๆ ไม่ทำงานทำการหรอ อ้าวว่าแต่เค้าตู ทำงานดีกว่า
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2009-07-14 10:57:57 |
By :
plakrim |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
พี่ปลากริม ก็ไวใช่ย่อย จริงๆงานผมเจอ Bug ครับ เลยแวะมาอู้ได้บ่อยๆ หัวหน้าไม่อยู่อีกต่างหาก.....ยะฮู้ฮอฮฮฮฮ ![](/images/bbcode/grin.gif?v=1001)
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2009-07-14 11:09:06 |
By :
panyapol |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
ขอบคุณครับทุกคน
ไปเปิดเจอในเว็บต่างประเทศมา ลองเอามาให้ดูด้วยเผื่อใครอยากใช้
date1 = new Date();
date2 = new Date();
//... เซ็ดค่า Date ตามสบายครับ ...
date1.setFullYear(2009,7,13);
date2.setFullYear(2009,7,14);
var one_day = 1000*60*60*24;
var defDate = (date2.getTime() - date1.getTime()) / one_day
//คำตอบได้ defDate = 1 ถูกต้องนะคร๊าบ
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2009-07-14 12:58:38 |
By :
terngjar |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
Code (JavaScript DateDiff)
//date2
//date1
var daydiff = (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24);
alert(daydiff);
Or
Code (JavaScirpt DateDiff)
var date1 = '24/11/2010';
var date2 = '26/11/2010';
date1 = date1.split("/");
date2 = date2.split("/");
sDate = new Date(date1[2],date1[1]-1,date1[0]);
eDate = new Date(date2[2],date2[1]-1,date2[0]);
var daysDiff = Math.round((eDate-sDate)/86400000);
Go to : ช่วยทีค้าบบ จะ เอาไปทำ เป็น java script ยังไงคับ code จากพี่ ๆ ในเวปนี้แหละคับ(ไปเจอมา)
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2011-02-10 23:43:59 |
By :
webmaster |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
ถ้าจะเทียบเวลาใกล้เคียงกับเวลาปัจจุบัน เช่น ตอนนี้เวลา 11.05 เราจะเทียบใกล้เคียงคือ น้อยกว่า 30 นาที กับมากกว่า 30 นาทีทำยังไงค่ะ ภาษา javascrip ค่ะ ขอบคุณล่วงหน้าค่ะ
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2011-08-17 11:06:42 |
By :
jack |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
อยากได้โค้ดอันนี้แบบ VB 2010 อ่ะคับ ขอบคุงคับ
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2012-04-29 02:22:32 |
By :
TheSkyPy |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
Code (JavaScript)
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
var dString = "May, 20, 1984";
var d1 = new Date(dString);
var d2 = new Date();
document.write("<br />Number of <b>days</b> since "+dString+": "+DateDiff.inDays(d1, d2));
document.write("<br />Number of <b>weeks</b> since "+dString+": "+DateDiff.inWeeks(d1, d2));
document.write("<br />Number of <b>months</b> since "+dString+": "+DateDiff.inMonths(d1, d2));
document.write("<br />Number of <b>years</b> since "+dString+": "+DateDiff.inYears(d1, d2));
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2012-06-08 09:17:43 |
By :
qua |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
|
|
|
![](/images/digitalocean-banner.jpg)
|
Load balance : Server 02
|