• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JS常用的时间处理方法

武飞扬头像
冰糖果粒橙
帮助1

目录

1. 时间格式化 -- 转换为:yyyy-MM-dd hh:mm:ss格式

2. 某一天所在星期范围

3. 获取当月第一天

4. 获取当月最后一天

5. 获取这个季度的第一天

6. 获取当年的第一天

7. 获取当前日期为一年中的第几天

8. 获取今天还剩多少天

9. 获取今天为周几

10.当前直接转换为当前,昨天,以往时间 (针对于聊天室)


1. 时间格式化 -- 转换为:yyyy-MM-dd hh:mm:ss格式

// 说明:JS时间Date格式化参数
// 参数:格式化字符串如:'yyyy-MM-dd hh:mm:ss'
// 结果:如2016-06-01 10:09:00
  1.  
    Date.prototype.format = function(fmt) {
  2.  
    let o = {
  3.  
    "M ": this.getMonth() 1, //月份
  4.  
    "d ": this.getDate(), //
  5.  
    "h ": this.getHours(), //小时
  6.  
    "m ": this.getMinutes(), //
  7.  
    "s ": this.getSeconds(), //
  8.  
    "q ": Math.floor((this.getMonth() 3) / 3), // 季度
  9.  
    "S": this.getMilliseconds() // 毫秒
  10.  
    };
  11.  
     
  12.  
    // 根据y的长度来截取年
  13.  
    if (/(y )/.test(fmt)) {
  14.  
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() "").substr(4 - RegExp.$1.length));
  15.  
    }
  16.  
    for (let k in o) {
  17.  
    if (new RegExp("(" k ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
  18.  
    "00" o[k]).substr(("" o[k]).length)));
  19.  
    }
  20.  
    return fmt;
  21.  
    }
  22.  
    // 用法:
  23.  
    let time1 = new Date().format("yyyy-MM-dd");
  24.  
    let time2 = new Date(1469281964000).format("yyyy-MM-dd hh:mm:ss");
学新通

2. 某一天所在星期范围

// 参数:'2022-03-04' || 时间对象
// 结果:'2022-03-04 至2022-03-10'
  1.  
    function getWeekRange(date) {
  2.  
    if (!date) return
  3.  
    let now = new Date(date);
  4.  
    let nowDayOfWeek = now.getDay(); // 星期日—>六(0->6
  5.  
    let nowDay = now.getDate();
  6.  
    let nowMonth = now.getMonth();
  7.  
    let nowYear = now.getYear();
  8.  
    nowYear = (nowYear < 2000) ? 1900 : 0;
  9.  
    let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek 1); // 这周的周五
  10.  
    let weekEndDate = new Date(nowYear, nowMonth, nowDay (7 - nowDayOfWeek)); // 7- 这周的周日 &&5- 得到这的周五
  11.  
    return weekStartDate.format('yyyy-MM-dd') " 至 " weekEndDate.format('yyyy-MM-dd');
  12.  
    }
  13.  
    // 用法:
  14.  
    getWeekRange('2022-03-05') //2022-03-042022-03-10

3. 获取当月第一天

//获取当月第一天
  1.  
    function getFirstDayOfMonth () {
  2.  
    let date = new Date();
  3.  
    date.setDate(1);
  4.  
    return date.format('yyyy-MM-dd');
  5.  
    }

4. 获取当月最后一天

  1.  
    function getFirstDayOfMonth () {
  2.  
    let date = new Date();
  3.  
    let month = date.getMonth();
  4.  
    date.setMonth(month 1);
  5.  
    date.setDate(0);
  6.  
    return date.format('yyyy-MM-dd');
  7.  
    }

5. 获取这个季度的第一天

  1.  
    function getFirstDayOfSeason() {
  2.  
    let date = new Date();
  3.  
    let month = date.getMonth();
  4.  
    if (month < 3) {
  5.  
    date.setMonth(0);
  6.  
    } else if (2 < month && month < 6) {
  7.  
    date.setMonth(3);
  8.  
    } else if (5 < month && month < 9) {
  9.  
    date.setMonth(6);
  10.  
    } else if (8 < month && month < 11) {
  11.  
    date.setMonth(9);
  12.  
    }
  13.  
    date.setDate(1);
  14.  
    return date.format('yyyy-MM-dd');;
  15.  
    }
学新通

6. 获取当年的第一天

  1.  
    function getFirstDayOfYear() {
  2.  
    let date = new Date();
  3.  
    date.setDate(1);
  4.  
    date.setMonth(0);
  5.  
    return date.format('yyyy-MM-dd');;
  6.  
    }

7. 获取当前日期为一年中的第几天

Math.ceil(( new Date() - new Date(new Date().getFullYear().toString()))/(24*60*60*1000)) 1;

8. 获取今天还剩多少天

  1.  
    function restDayOfYear() {
  2.  
    let fullyear = new Date().getFullYear();
  3.  
    let nextyear = fullyear 1;
  4.  
    let lastday = new Date(new Date(nextyear, 0, 1) - 1); //本年的最后一毫秒:
  5.  
    let now = new Date();
  6.  
    let diff = lastday - now; //毫秒数
  7.  
     
  8.  
    return Math.ceil(diff / (1000 * 60 * 60 * 24));
  9.  
    }

9. 获取今天为周几

  1.  
    getWeekZh(date) {
  2.  
    let weekArrayList = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  3.  
    let index = new Date(`${date}`).getDay()
  4.  
    let week = weekArrayList[index]
  5.  
    return week
  6.  
    }

10.当前直接转换为当前,昨天,以往时间 (针对于聊天室)

  1.  
    dateTime(e) {
  2.  
    let old = new Date(e);
  3.  
    let now = new Date();
  4.  
    //获取old具体时间
  5.  
    let d = old.getTime();
  6.  
    let h = old.getHours();
  7.  
    let m = old.getMinutes();
  8.  
    let Y = old.getFullYear();
  9.  
    let M = old.getMonth() 1;
  10.  
    let D = old.getDate();
  11.  
    //获取now具体时间
  12.  
    let nd = now.getTime();
  13.  
    let nh = now.getHours();
  14.  
    let n = now.getMinutes();
  15.  
    let nY = now.getFullYear();
  16.  
    let nM = now.getMonth() 1;
  17.  
    let nD = now.getDate();
  18.  
     
  19.  
    //当天的时间
  20.  
    if (D === nD && M === nM && Y === nY) {
  21.  
    if (h < 10) {
  22.  
    h = '0' h;
  23.  
    }
  24.  
    if (m < 10) {
  25.  
    m = '0' m;
  26.  
    }
  27.  
    return h ':' m;
  28.  
    }
  29.  
    //昨天时间
  30.  
    if (D 1 === nD && M === nM && Y === nY) {
  31.  
    if (h < 10) {
  32.  
    h = '0' h;
  33.  
    }
  34.  
    if (m < 10) {
  35.  
    m = '0' m;
  36.  
    }
  37.  
    return '昨天 ' h ':' m;
  38.  
    } else if (Y == nY) {
  39.  
    //今年
  40.  
    if (h < 10) {
  41.  
    h = '0' h;
  42.  
    }
  43.  
    if (m < 10) {
  44.  
    m = '0' m;
  45.  
    }
  46.  
    return M '月' D '日 ' h ':' m
  47.  
    } else {
  48.  
    //大于今年
  49.  
    if (h < 10) {
  50.  
    h = '0' h;
  51.  
    }
  52.  
    if (m < 10) {
  53.  
    m = '0' m;
  54.  
    }
  55.  
    return Y '年' M '月' D '日 ' h ':' m
  56.  
    }
  57.  
    },
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgheeaj
系列文章
更多 icon
同类精品
更多 icon
继续加载