้เช่น 7/4/2004 ถึง 7/30/2011 (รูปแแบบ ด/ว/ปี คศ.) ให้ผลเป็น 7 ปี 0 เดือน 26 วัน
<%
'find agejob
set rs1=Server.CreateObject("ADODB.recordset")
sql="SELECT emp_begindate FROM tblemployee "
sql=sql & "WHERE empid='" & id &"';"
rs1.Open sql, conn
%>
<script language=JavaScript>
var startDate="<%=rs1.fields("emp_begindate")%>"
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()]
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;
}
return between ;
}
var curDate=new Date();
var myDate=new Date();
var endDate=new Date();
sp=startDate.split("/");
var dd=Number(sp[2]);
//alert(dd);
if (dd > 2500) //แก้ไขเมือปี 2600 อีกที
{
sp[2]=sp[2]-543;
}
startDate=sp[1]+"-"+sp[0]+"-"+sp[2];
endDate=(myDate.getMonth()+1)+"-"+myDate.getDate()+"-"+myDate.getFullYear();
//alert(startDate+" " + endDate);
var until = getTimeBetween(startDate, endDate) ; //เปลี่ยนวันที่ตรงนี้ m/d/year
//alert(until[0] + " years " + until[1] + " months "+ until[2] + " days " + until[3]+" hours " + until[4] + " minutes " + until[5] + " seconds");
document.getElementById("agejob").innerHTML=until[0]+" ปี "+until[1]+" เดือน "+ until[2]+" วัน";
<%rs1.close%>
</script>
ด้านล่างให้เครดิตแก่ http://www.thaicreate.com/php/forum/029180.html
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 ; } 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");
ไม่มีความคิดเห็น:
แสดงความคิดเห็น