<html> <head> <meta http-equiv="Content-Type" content="textml; charset=utf-8"> <title>js获取日期:前天、昨天、今天、明天、后天 ,并比较时间大小</title> </head> <body> <script language="JavaScript" type="text/javascript"> function GetDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期 var y = dd.getFullYear(); var m = dd.getMonth()+1;//获取当前月份的日期 var d = dd.getDate(); return y+"-"+m+"-"+d; } document.write("3天前:"+GetDateStr(-3)); document.write("<br />昨天:"+GetDateStr(-1)); document.write("<br />今天:"+GetDateStr(0)); document.write("<br />明天:"+GetDateStr(1)); document.write("<br />后天:"+GetDateStr(2)); document.write("<br />大后天:"+GetDateStr(3)); function CompareDate(d1,d2){ return ((new Date(d1.replace(/-/g,"\/"))) > (new Date(d2.replace(/-/g,"\/")))); } alert(GetDateStr(0)+"大于"+GetDateStr(1)+"吗?"+CompareDate(GetDateStr(0),GetDateStr(1))); </script> </body> </html>
时间: 2024-10-13 20:30:48