0.1、JS-時間處理積累

bestcxx發表於2020-11-09

測試時間戳 1604740691000

1604740691000

時間戳轉時間

var date = new Date(1604740691000);
console.info(date.toLocaleString());
輸出:2020/11/7 下午5:18:11

獲取當前時間

var date = new Date();
console.info(date.toLocaleString());
輸出:2020/11/9 下午8:16:57

獲取今天

var date = new Date();
var today=date.toLocaleString().substr(0,10);
console.info(today);
輸出:2020/11/9 

按照指定格式輸出日期

var date = new Date();
console.info(date.getFullYear().toString()+'年'+date.getMonth().toString()+'月'+date.getDay().toString()+'日');
年月日:2020101

按照指定格式輸出 年月日時分秒

var date = new Date();
console.info(date.getFullYear().toString()+'年'+date.getMonth().toString()+'月'+date.getDay().toString()+'日'+' '+date.getHours().toString()+':'+date.getMinutes().toString()+':'+date.getSeconds().toString());
VM2369:2 202010121:5:45

獲取星期 星期日-0,星期一-1,星期二-2…星期六-6

var date = new Date();
console.info(date.getDay());
輸出:0

獲取星期中文

function getWeekDay(date){ 
switch (date.getDay()) {
    case 0:
        day = "星期天";
        break;
    case 1:
        day = "星期一";
         break;
    case 2:
        day = "星期二";
         break;
    case 3:
        day = "星期三";
         break;
    case 4:
        day = "星期四";
         break;
    case 5:
        day = "星期五";
         break;
    case 6:
        day = "星期六";
} 
return day;

}
console.info(getWeekDay(new Date()));

輸出: 星期天

判斷是否是今天

function check(date){
  if(date.toLocaleString().substr(0,10)==(new Date().toLocaleString().substr(0,10))){
		return true;
	}else{
		return false;
	}
}

判斷是否是昨天

 function isYesterday(time) {
		 const date = new Date();
	     const year = date.getFullYear();
	    const month = date.getMonth() + 1;
	    const day = date.getDate();
	    const today = `${year}/${month}/${day}`; 
        const todayTime = new Date(today).getTime(); // 當天凌晨的時間
        const yesterdayTime = new Date(todayTime - 24 * 60 * 60 * 1000).getTime(); // 昨天凌晨的時間
        return time < todayTime && yesterdayTime <= time;
    }
console.info(isYesterday(1604740691000));
輸出: false

判斷是否是去年

function isLastYear(time){
    const year = new Date().getFullYear();
    const lastYear = new Date(time).getFullYear();
    return (year - lastYear) === 1;

}
console.info(isYesterday(1604740691000));
輸出: false

判斷是否是前天

function isLastYesterday(time) {
    const twentyFourHours = 24 * 60 * 60 * 1000;
    const fortyEightHours = 24 * 60 * 60 * 1000 * 2;
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const today = `${year}/${month}/${day}`; 
    const todayTime = new Date(today).getTime(); // 當天凌晨的時間
    const yesterdayTime = new Date(todayTime - twentyFourHours).getTime(); // 昨天凌晨的時間
    const lastYesterdayTime = new Date(todayTime - fortyEightHours).getTime(); // 昨天凌晨的時間
    return time < yesterdayTime && lastYesterdayTime <= time;
}
console.info(isYesterday(1604740691000));
輸出: false

參考

1、https://blog.csdn.net/m0_37792830/article/details/90666956
2、https://www.cnblogs.com/ukzq/p/10321377.html
3、https://www.cnblogs.com/chenrenshui/p/6009152.html

相關文章