js获取date常用的时间(基本满足大部分应用场景)

功能函数介绍
1 datesFn.dateFormat(date,string) 传入new Date()得到任意时间的格式数据[年/月/日 时/分/秒]
例:datesFn.dateFormat(new Date(),"Y-M-D")    //2022-04-27 
例:datesFn.dateFormat(new Date(),"Y-M-D H:M:S")   //2022-04-27 15:28:06

获取某时间是周几
获取某一天距离今天的时间
获取某月第一天的时间
获取某月最后一天的时间
通过两个日期获取之间的日期数组列表[2021-05-12,2021-05-13,...]
获取某一天距离今天的时间
获取上一天、后一天、上个月、下个月、上一年、后一年的时间
获取30天前的时间

封装不好 凑活看吧 回头有空了我再多加点注释

let datesFn = {
  /* 每天毫秒数 */
  dayTime:1000*60*60*24,

  /* 日期格式加工处理 */
  dateFormat:function(date,type) {
    if(typeof date === 'string'){ date = new Date(date) }
    let data = {};
    data.n = date.getFullYear() + '';
    data.y = this.addWei(date.getMonth() + 1) + '';
    data.r = this.addWei(date.getDate()) + '';
    data.s = this.addWei(date.getHours()) + '';
    data.f = this.addWei(date.getMinutes()) + '';
    data.m = this.addWei(date.getSeconds()) + '';
    if(type === 'Y-M-D H:M') { return data.n + '-' + data.y + '-' + data.r + ' ' + data.s + ':' + data.f }
    if(type === 'Y-M-D H:M:S') { return data.n + '-' + data.y + '-' + data.r + ' ' + data.s + ':' + data.f + ':' + data.m }
    else if(type === 'Y-M-D') { return data.n + '-' + data.y + '-' + data.r }
    else if(type === 'Y-M') { return data.n + '-' + data.y }
    else if(type === 'Y') { return data.n }
    else if(type === 'M') { return data.y }
    else if(type === 'D') { return data.r }
    else if(type === 'H:M') { return data.s + ':' + data.f }
    else if(type === 'H:M:S') { return data.s + ':' + data.f + ':' + data.m }
    else { return data; }
  },
  /*单位处理为双位()*/
  addWei:function (data) { if(data <10){ data = "0" + data; } return data; },

  /* 获取某一天距离今天的时间 */
  /* 获取某月第一天 传入某月任意date*/
  startDay:function(date){
    let firstDay = this.dateFormat(new Date(date.getFullYear(),date.getMonth(),1),'Y-M-D');
    return firstDay
  },
  /* 获取某月最后一天 传入某月任意date*/
  endDay:function(date){
    let nextMonth_firstDay = new Date(date.getFullYear(),date.getMonth()+1,1); /* 下个月的第一天*/
    let endDay = this.dateFormat(new Date(nextMonth_firstDay - this.dayTime),'Y-M-D');
    return endDay
  },
  /* 通过两个日期获取之间的日期数组集合 */
  startEndDates(startDate,endDate,type){
    let dates = [];
    let presentDate = new Date(startDate).getTime();
    const endDateGetTime = new Date(endDate).getTime()
    while (presentDate <= endDateGetTime ){
      if(type ==='D'){ dates.push(this.dateFormat(new Date(presentDate),'D')); presentDate += this.dayTime }
      else { dates.push(this.dateFormat(new Date(presentDate),'Y-M-D')); presentDate += this.dayTime }
    }
    return dates
  },

  /* 获取某时间是周几 */
  getWeek:function (date,type){
    const stringList = ["日", "一", "二", "三", "四", "五", "六"];
    const numberList = [7, 1, 2, 3, 4, 5, 6];
    let day = date.getDay();
    let str = null;
    if(type === 'initiatoryNumber'){ str = day }
    if(type === '周'){ str = type + stringList[day]; }
    if(type === '星期'){ str = type + stringList[day]; }
    else { str = numberList[day]; }
    return str
  },

  /* 上一天 */
  leftDay:function (date){ return this.dateFormat(new Date(date.getTime() - 24 * 60 * 60 * 1000),'Y-M-D') },
  /* 下一天 */
  rightDay:function (date){ return this.dateFormat(new Date(date.getTime() + 24 * 60 * 60 * 1000),'Y-M-D') },

  /* 上个月 */
  leftMonth:function (date){
    return this.dateFormat(new Date(date.getFullYear(),date.getMonth()-1,1),'Y-M')
  },
  /* 下个月 */
  rightMonth:function (date){
    return this.dateFormat(new Date(date.getFullYear(),date.getMonth()+1,1),'Y-M')
  },
  /* 上年 */
  leftYear:function (date){
    return this.dateFormat(new Date(date.getFullYear()-1,date.getMonth(),1),'Y-M')
  },
  /* 下年 */
  rightYear:function (date){
    return this.dateFormat(new Date(date.getFullYear()+1,date.getMonth(),1),'Y-M')
  },
  /* 30天前 */
  left30Day:function (date){
    return this.dateFormat(new Date(date.getTime() - 24 * 60 * 60 * 1000 * 30),'Y-M-D H:M:S')
  },
}

创作不易   请不要复制后 反手一个收费下载

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值