js获取当前月的第一天和最后一天
程序员文章站
2024-01-28 09:17:16
...
js获取当前月的第一天和最后一天:
function getFirstAndLastMonthDay( year, month){
var firstdate = year + '-' + month + '-01';
var day = new Date(year,month,0);
var lastdate = year + '-' + month + '-' + day.getDate();//获取当月最后一天日期
//给文本控件赋值。同下
return lastdate;
}
js将String类型转换为Date类型:
function string2Date(aStringDate){
var datepattern=/^((\d{2}(([02468][048])|([13579][26]))\-((((0[13578])|(1[02]))\-((0[1-9])|([1-2][0-9])|(3[01])))|(((0[469])|(11))\-((0[1-9])|([1-2][0-9])|(30)))|(02\-((0[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))\-((((0[13578])|(1[02]))\-((0[1-9])|([1-2][0-9])|(3[01])))|(((0[469])|(11))\-((0[1-9])|([1-2][0-9])|(30)))|(02\-((0[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0[0-9])|(1[0-9])|(2[0-3]))\:([0-5][0-9])))$/;
if(!datepattern.exec(aStringDate)){
alert("非法日期格式!");
return;
}
var year=Number(aStringDate.substring(0,4));
var month=Number(aStringDate.substring(5,7));
month--;
var mydate=Number(aStringDate.substring(8,10));
var hour=Number(aStringDate.substring(11,13));
var minute=Number(aStringDate.substring(14,16));
return new Date(year,month,mydate,hour,minute);
}
js将Date类型转换为String类型:
function date2String(aDate){
var year=aDate.getYear();
var month=aDate.getMonth();
month++;
var mydate=aDate.getDate();
var hour=aDate.getHours();
var minute=aDate.getMinutes();
var mytime=year+"-"+(month<10?"0":"")+month+"-"+(mydate<10?"0":"")+mydate+" "+(hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute;
return mytime;
}
下一篇: js基础部分(1)