跨年操作--new Date()
//時間在2017/12/31 17:00 --- 2018/1/1 06:00區間,提示用戶無法操作公告。
//time.js
(function(){
var date = new Date();
//當前年份
?? ? ? var year = date.getFullYear();
?? ? ? //當前月份
?? ? ? var month = date.getMonth()+1;
?? ? ? //當前日
?? ? ? var day = date.getDate();
?? ? ? //當前小時
?? ? ? var hour = date.getHours();
?? ? ? //當前分鐘
?? ? ? var minute = date.getMinutes();
?? ? ? //當前秒
?? ? ? var second = date.getSeconds();
?? ? ? //當前日期時間
?? ? ? var timedate = date.toLocaleString();?
? ? ? ?
?? ? ? var config ={
?? ? ? starttime : "2017-12-31_17:00",//開始時間
?? ? ? endtime:"2018-01-1_06:00" ? ? ? //結束時間
? ? ? ?
?? ? ? }
?? ? ? var start = config.starttime.split("_");?
?? ? ? var end = config.endtime.split("_");
? ? ? ?
?? ? ? var yearFlag = start[0].split("-")[0];
?? ? ? var nextYear = end[0].split("-")[0];
? ? ? ?
?? ? ? var monthFlag = start[0].split("-")[1];
?? ? ? var nextMonth = end[0].split("-")[1];
? ? ? ?
?? ? ? var dayFlag = start[0].split("-")[2];
?? ? ? var nextDay = end[0].split("-")[2];
? ? ? ?
?? ? ? var hourFlag =? start[1].split(":")[0];
?? ? ? var nextHour = end[1].split(":")[0];
? ? ? ?
?? ? ? var minuteFlag =? start[1].split(":")[1];
?? ? ? var nextMinute = end[1].split(":")[1];
? ? ? ?
?? ? ? console.log('date',date);
?? ? ? console.log('year',typeof(year));
?? ? ? console.log('month',month);
?? ? ? console.log('day',day);
?? ? ? console.log('hour',typeof(hour));
?? ? ? console.log('minute',minute);
?? ? ? console.log('second',second);
?? ? ? console.log('當前日期時間',timedate);
? ? ? ?
?? ? ? //判斷當前時間是否在2017/12/31 17:00 --- 2018/1/1 06:00區間
?? ? ? if(year != yearFlag){ //2018年
?? ? ? if(month != nextMonth){
?? ? ? return;
?? ? ? }else{ //等于1月
?? ? ? if(day != nextDay){
?? ? ? return;
?? ? ? }else{ //等于1日
?? ? ? if(hour>=nextHour){
?? ? ? return;
?? ? ? }
?? ? ? }
?? ? ? }
?? ? ? }else{ //2017年
?? ? ? if(month < monthFlag || month > nextMonth){
?? ? ? return;
?? ? ? }else{ //等于12月
?? ? ? if(day < dayFlag || day > nextDay){?
?? ? ? return;
?? ? ? }else{ //等于31日
?? ? ? if(hour < hourFlag || hour>nextHour){
?? ? ? return;
?? ? ? }else{ //大于等于17點
?? ? ? if(minute < minuteFlag || minute > nextMinute){
?? ? ? return;
?? ? ? }
?? ? ? }
?? ? ? }
?? ? ? }
? ? ? ?
?? ? ? }
? ? ? ?
?? ? ? location.href="https://xxx/error.html";
})();
?
轉載于:https://www.cnblogs.com/Super-scarlett/p/8176193.html
總結
以上是生活随笔為你收集整理的跨年操作--new Date()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】windows server 20
- 下一篇: SPSS新手教程—两步聚类之结果解读