zoukankan      html  css  js  c++  java
  • JS常用时间处理方法

    这里会扩展一些JS常用时间处理方法,内置时间对象的方法不再赘述 —— 传送门:http://www.w3school.com.cn/js/jsref_obj_date.asp

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

    // 说明:JS时间Date格式化参数
    // 参数:格式化字符串如:'yyyy-MM-dd hh:mm:ss'
    // 结果:如2016-06-01 10:09:00
    Date.prototype.format = function (fmt) { 
        var o = {
            "M+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "h+": this.getHours(), //小时 
            "m+": this.getMinutes(), //分 
            "s+": this.getSeconds(), //秒 
            "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
            "S": this.getMilliseconds() // 毫秒
        };
    
        // 根据y的长度来截取年
        if (/(y+)/.test(fmt)){ 
    	fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o){
    	if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
        return fmt;
    }
    // 用法: 
    var time1 = new Date().format("yyyy-MM-dd");
    var time2 = new Date(1469281964000).format("yyyy-MM-dd hh:mm:ss");
    console.log(time1);
    console.log(time2);
    

    某一天所在星期范围

    // 参数:‘2019-03-05’ || 时间对象
    // 结果:‘2019-03-04 至 2019-03-10’
    function getWeekRange(date) {
      if(!date) return
    
      var now = new Date(date);
      var nowDayOfWeek = now.getDay();  // 星期日—>六(0->6)
      var nowDay = now.getDate();
      var nowMonth = now.getMonth();
      var nowYear = now.getYear(); // 2019年是119
      nowYear += (nowYear < 2000) ? 1900 : 0;
      var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek+1);  // 这周的周五
      var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));   // 7- 这周的周日  &&  用5- 得到这的周五
      return weekStartDate.format('yyyy-MM-dd') + " 至 " + weekEndDate.format('yyyy-MM-dd');
    }
    // 用法:
    getWeekRange(‘2019-03-05’)  // ‘2019-03-04 至 2019-03-10’
    

    获取当月第一天

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

    获取当月最后一天

    function getFirstDayOfMonth () {
        var date = new Date();
        var month = date.getMonth();
        date.setMonth(month+1);
        date.setDate(0);
        return date.format('yyyy-MM-dd');
    }
    

    获取这个季度的第一天

    function getFirstDayOfSeason () {
        var date = new Date();
        var month = date.getMonth();
        if(month <3 ){
            date.setMonth(0);
        }else if(2 < month && month < 6){
            date.setMonth(3);
        }else if(5 < month && month < 9){
            date.setMonth(6);
        }else if(8 < month && month < 11){
            date.setMonth(9);
        }
        date.setDate(1);
        return date.format('yyyy-MM-dd');;
    }
    

    获取当年的第一天

    function getFirstDayOfYear () {
        var date = new Date();
        date.setDate(1);
        date.setMonth(0);
        return date.format('yyyy-MM-dd');;
    }
    

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

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

    获取今天还剩多少天

    function restDayOfYear() {
        var fullyear = new Date().getFullYear();
        var nextyear = fullyear + 1;
        var lastday = new Date(new Date(nextyear,0,1) - 1); //本年的最后一毫秒:
        var now = new Date();
        var diff = lastday - now;  //毫秒数
    
        return Math.ceil(diff / (1000 * 60 * 60 * 24));
    }
    

    获取今天星期几中文

    getWeekZh(value) {
       var dateArray = value.split("-");
       var zhWeek = "星期" + "日一二三四五六".charAt(new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]).getDay());
       return zhWeek
    }
    

    未完待续...

  • 相关阅读:
    商城02——dubbo框架整合_商品列表查询实现_分页
    商城项目01——工程介绍及搭建
    利用ssm框架做一个客户管理系统
    SpringMVC学习笔记
    spring问题
    Spring学习笔记
    MyBatis学习笔记二
    MyBatis学习笔记
    二分查找与几种排序方法
    配置 spring boot 的 banner (自定义或取消banner)
  • 原文地址:https://www.cnblogs.com/chenwenhao/p/10480234.html
Copyright © 2011-2022 走看看