日期:2014-05-16  浏览次数:20408 次

JS_日期处理

得到当前日期 ,格式“yyyy-MM-dd”

var date = new Date();	
var month = (date.getMonth()+1) < 10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
var day = (date.getDate()) < 10 ? "0"+date.getDate() : date.getDate();
var today = date.getFullYear()+"-"+month+"-"+day;

?

?

JS Date API: http://www.cnblogs.com/jianshao810/archive/2010/09/09/1821861.html

?


var datePattern=/^(\d{4})-(\d{2})-(\d{2})$/;
datePattern.test(trim('2011-12-12')) //正则验证YYYY-MM-DD样式

/*
两个日期比较大小
参数:
fristDate:第一个日期
secondDate:第二个日期
*/
function compareDate(fristDate,secondDate){
if(fristDate==null || fristDate=="" || !datePattern.test(trim(fristDate))){
alert("日期格式不对,必须为YYYY-MM-DD样式。(compareDate)");
return;
}
if(secondDate==null || secondDate=="" || !datePattern.test(trim(secondDate))){
alert("日期格式不对,必须为YYYY-MM-DD样式。(compareDate)");
return;
}
if(fristDate>secondDate) return 1;
else if(fristDate==secondDate) return 0;
else return -1;

}


/**
* 日期增加相应的天数,返回格式为YYYY-MM-DD的字符串
* p_date 格式为:YYYY-MM-DD 或 YYYY/MM/DD
* add_date( '2011-12-12', '3')
**/
function add_date(p_date, p_day){
var new_Date = new Date(p_date.replace(/-/g,"\/"));
var lIntval = parseInt(p_day,10)//间隔
new_Date.setDate(new_Date.getDate() + lIntval)

var yy1 = new_Date.getFullYear(); //注意这个地方一定是 getFullYear 不是 getYear
var mm1 = new_Date.getMonth()+1;
if(mm1<10)
mm1 = "0"+mm1.toString();
var dd1 = new_Date.getDate();
if(dd1<10)
dd1 = "0"+dd1.toString();
return yy1+"-"+mm1+"-"+dd1;
}

//也可以这样处理日期
var selectdate = "2011-11-02";
var sdate = new Date();
sdate.setYear(parseInt(selectdate.substring(0,4)));
sdate.setMonth(parseInt(selectdate.substring(5,7)));
sdate.setDate(parseInt(selectdate.substring(8,10)));

?


1。解决2000问题
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
2。检查日期是否合法
// 当输入参数为isDate(dd,mm,ccyy)时,表示要检查年,月,日
// 当输入参数为isDate(dd,mm) 表示默认年为当前年
// 当输入参数为isDate(dd) 表示默认年,月为当前年月
// 注意输入月份保证在1-12以内。
function isDate (day,month,year) {
var today = new Date();
year = ((!year) ? y2k(today.getYear())year);
month = ((!month) ? today.getMonth():month-1);
if (!day) return false
var test = new Date(year,month,day);
if ( (y2k(test.getYear()) == year) &&
(month == test.getMonth()) &&
(day == test.getDate()) )
return true;
else
return false
}

以下是调用例子:
if (isDate(31,2,1997))
document.write("Valid");
else
document.write("Invalid");

3。如何判断两个日期中的间隔天数
function daysElapsed(date1,date2) {
var difference = Date.UTC(date1.getYear(),date1.getMonth(),date1.getDate(),0,0,0)
- Date.UTC(date2.getYear(),date2.getMonth(),date2.getDate(),0,0,0);
return difference/1000/60/60/24;
}

4。如何将一个下拉列表框中的月份传递到另一页
<FORM>
<SELECT NAME="selectName">
<OPTION>January
<OPTION>February
<OPTION>March
<OPTION>April
<OPTION>May
<OPTION>June
<OPTION>July
<OPTION>August
<OPTION>Spetember
<OPTION>October
<OPTION>November
<OPTION>December
</SELECT>
<INPUT TYPE="BUTTON" VALUE="Go" onClick="window.location.href = ''nextpage.html?'' +
this.form.selectName.options[this.form.selectName.selectedIndex].text">
</FORM>

在nextpage.html中加入下面的代码
<FORM name="formName"><INPUT TYPE="TEXT" name="textName"><FORM>
<SCRIPT LANGUAGE="JavaScript"><
document.formName.textName.value = location.search.substring(1);
//-SCRIPT>

或则:
<SCRIPT LANGUAGE="JavaScript"><
document.write("<FORM><INPUT TYPE=''TEXT'' ");
document.write("VALUE=''"location.search.substring(1)+"FORM>")
//-SCRIPT>

5。如何将一个字符串中的时间和当前时间做比较
<SCRIPT LANG