0.1、JS-时间处理积累

测试时间戳 1604740691000

1604740691000

js 实体排序


if(list.length>0){
      list.sort(compare("gmtCreated"));
    }
    
//比较大小函数
function compare(propertyName) {
  return function (object1, object2) {
  var value1 = object1[propertyName];
  var value2 = object2[propertyName];
  if (value2 < value1) {
    return -1;
  }
  else if (value2 > value1) {
    return 1;
  }
  else {
    return 0;
  }
  }
}

时间戳转时间

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()+1).toString()+'月'+date.getDate().toString()+'日');
输出:20201116

按照指定格式输出 年月日时分秒

var date = new Date();
console.info(date.getFullYear().toString()+'年'+(date.getMonth()+1).toString()+'月'+date.getDate().toString()+'日'+' '+date.getHours().toString()+':'+date.getMinutes().toString()+':'+date.getSeconds().toString());
输出: 2020111614:56:11

获取星期 星期日-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) {
  var twentyFourHours = 24 * 60 * 60 * 1000;
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var today = year+'/'+month+'/'+day; 
  var todayTime = new Date(today).getTime(); // 当天凌晨的时间
  var yesterdayTime = new Date(todayTime - twentyFourHours).getTime(); // 昨天凌晨的时间
  return (time < todayTime) && (yesterdayTime <= time);
  }
console.info(isYesterday(1604740691000));
输出: false

判断是否是去年 及更久

//是否去年以及更久
function isLastYear(time){
    var year = new Date().getFullYear();
    var lastYear = new Date(time).getFullYear();
    return ((year - lastYear) >= 1);//=1是去年,>1 是更久
}
console.info(isYesterday(1604740691000));
输出: false

判断是否是前天

//是否前天
function isLastYesterday(time) {
    var twentyFourHours = 24 * 60 * 60 * 1000;
    var fortyEightHours = 24 * 60 * 60 * 1000 * 2;
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var today = year+'/'+month+'/'+day; 
    var todayTime = new Date(today).getTime(); // 当天凌晨的时间
    var yesterdayTime = new Date(todayTime - twentyFourHours).getTime(); // 昨天凌晨的时间
    var 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

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页