獲取明天的時間戳
阿新 • • 發佈:2019-01-31
最近剛做了一個H5的專案,專案需求是希望預約時間最晚為明天18點前,通過寫了個簡單的驗證方法來執行。
var time = $("#time").val();//選擇框內的值
var tomorrowd=new Date();//當前時間
var hourTime = new Date(time.replace(/\-/g, "\/"));//替換"-"為"/"的當前時間
var d1 = new Date(time.replace(/\-/g, "\/"));//替換"-"為"/"的選擇時間
//setDate() 方法用於設定一個月的某一天。
//在此設定明天的18:01:00
tomorrowd.setDate(tomorrowd.getDate()+1 );
tomorrowd.setHours(18);
tomorrowd.setMinutes(0);
tomorrowd.setSeconds(1);
tomorrowd.setMilliseconds(0);
//將時間轉換為時間戳
console.log('選擇的時間戳:'+d1.getTime());
console.log('明天18點的時間戳:'+tomorrowd.getTime());
console.log('半小時的時間戳:'+new Date().getTime()+ 1000*60*30);
//驗證判斷
if (time == null || time === '') {
webToast("請選擇預約時間!");
return false;
}
if (hourTime.getHours() < 9 || hourTime.getHours() > 18 ||(hourTime.getHours() === 18 && hourTime.getMinutes() !==0)){
webToast("請選擇工作時間9:00-18:00!");
return false;
}
if(d1.getTime() < new Date().getTime()+ 1000*60*30){
webToast("請選擇當前時間至少延後30分鐘!");
return false;
}
if(d1.getTime() > tomorrowd.getTime()){
webToast("預約時間最晚可選擇明天18點前!");
return false;
}