js預設當月日期以及時間比較
阿新 • • 發佈:2019-02-04
//將時間轉化成時間戳
function transdate(time){
var date=new Date();
date.setFullYear(time.substring(0,4));
date.setMonth(time.substring(5,7)-1);
date.setDate(time.substring(8,10));
return Date.parse(date);
}
//驗證起始時間
function checkTime(start, end){
start = transdate(start);
end = transdate(end);
if(start > end) {
alert('開始時間不能大於截止時間');
return false;
}
return true;
}
//表單驗證
function verifyForm(){
var start = $("#start_time").val();
var end = $("#end_time").val();
if(start!='' && end!='' &&!checkTime(start, end) ){
return false;
}
return true;
}
//根據年月獲取當月天數
function getDaysInMonth(year,month) {
month = parseInt(month,10);
var temp = new Date(year,month,0);
return temp.getDate();
}
function initTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var days = getDaysInMonth(year, month);
if(month < 9) {
month = '0'+month;
}
var start = $("#start_time").val();
var end = $("#end_time").val();
if(start == '' & end == ''){
$("#start_time").attr('value', year+'-'+month+'-01');
$("#end_time").attr('value', year+'-'+month+'-'+days);
}
}
function transdate(time){
var date=new Date();
date.setFullYear(time.substring(0,4));
date.setMonth(time.substring(5,7)-1);
date.setDate(time.substring(8,10));
return Date.parse(date);
}
//驗證起始時間
function checkTime(start, end){
start = transdate(start);
end = transdate(end);
if(start > end) {
alert('開始時間不能大於截止時間');
return false;
}
return true;
}
//表單驗證
function verifyForm(){
var start = $("#start_time").val();
var end = $("#end_time").val();
if(start!='' && end!='' &&!checkTime(start, end) ){
return false;
}
return true;
}
//根據年月獲取當月天數
function getDaysInMonth(year,month) {
month = parseInt(month,10);
var temp = new Date(year,month,0);
return temp.getDate();
}
function initTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var days = getDaysInMonth(year, month);
if(month < 9) {
month = '0'+month;
}
var start = $("#start_time").val();
var end = $("#end_time").val();
if(start == '' & end == ''){
$("#start_time").attr('value', year+'-'+month+'-01');
$("#end_time").attr('value', year+'-'+month+'-'+days);
}
}