首页 > 其他分享 >js-Date扩展format()函数--处理时间字符串格式

js-Date扩展format()函数--处理时间字符串格式

时间:2022-11-09 15:01:29浏览次数:43  
标签:const format -- js strMonth str date replace strDay

js-Date扩展format()函数--处理时间字符串格式

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}


处理月份和天的日期字符串,就是个位数前面加0的截取处理,或者两位数的截取处理
('0' + dateNum).substr(-2, 2)

//  js Date扩展Format()函数  处理时间字符串格式
/**
 * 实参formatStr格式为 'yyyy-MM-dd hh:mm:ss'
 * 实参formatStr里面的月份MM必须大写
 * 
 */
Date.prototype.format = function (formatStr) {
  var str = formatStr;
            var Week = ['日', '一', '二', '三', '四', '五', '六'];

            str = str.replace(/yyyy|YYYY/, this.getFullYear());
            str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));

            str = str.replace(/MM/, this.getMonth() > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
            str = str.replace(/M/g, this.getMonth());

            str = str.replace(/w|W/g, Week[this.getDay()]);

            str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
            str = str.replace(/d|D/g, this.getDate());

            str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
            str = str.replace(/h|H/g, this.getHours());
            str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
            str = str.replace(/m/g, this.getMinutes());

            str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
            str = str.replace(/s|S/g, this.getSeconds());

            return str;
};


//  var aa = new Date().format('yyyy-MM-dd hh:mm:ss')
 var aa = new Date().format('yyyy-MM-dd')
console.log(aa)

// 获取一个月前的时间
const getLastMonthYestdy = (date) => {
  // var date = new Date(); //        1    2    3    4    5    6    7    8    9   10    11   12月
  var daysInMonth = new Array([0], [31], [28], [31], [30], [31], [30], [31], [31], [30], [31], [30], [31]);
  var strYear = date.getFullYear();
  var strDay = date.getDate();
  var strMonth = date.getMonth() + 1;
  if (strYear % 4 == 0 && strYear % 100 != 0) { //一、解决闰年平年的二月份天数   //平年28天、闰年29天//能被4整除且不能被100整除的为闰年
    daysInMonth[2] = 29;
  }
  if (strMonth - 1 == 0) //二、解决跨年问题
  {
    strYear -= 1;
    strMonth = 12;
  } else {
    strMonth -= 1;
  }
  //        strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];
  strDay = Math.min(strDay, daysInMonth[strMonth]); //三、前一个月日期不一定和今天同一号,例如3.31的前一个月日期是2.28;9.30前一个月日期是8.30

  if (strMonth < 10) //给个位数的月、日补零
  {
    strMonth = "0" + strMonth;
  }
  if (strDay < 10) {
    strDay = "0" + strDay;
  }
  let datastr = strYear + "/" + strMonth + "/" + strDay;
  return datastr;

}
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatTimeHanzhi = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return year + '年' + month + '月' + day + '日 ' + hour + '时' + minute + '分' + second + '秒';
  // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

标签:const,format,--,js,strMonth,str,date,replace,strDay
From: https://www.cnblogs.com/lifan-fineDay/p/16873684.html

相关文章

  • 防抖与节流
    防抖和节流是针对响应跟不上触发频率这类问题的两种解决方案。在给DOM绑定事件时,有些事件我们是无法控制触发频率的。如鼠标移动事件onmousemove,滚动滚动条事件onscrol......
  • BloodHound学习
    一、LDAP目录服务(1)目录服务是一个特殊的数据库,用来保存描述性的、基于属性的详细信息,支持过滤功能(2)是动态的,灵活的,易扩展的。ex.电话簿、地址簿LDAP......
  • 队列
    一、队列的基本概念1、队列的定义队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。队列是一种先进先出(FirstInFirstOut)的线性表,简称FIFO。允许......
  • JS实现数组去重(重复的元素只保留一个)
    JS实现数组去重(重复的元素只保留一个)1.遍历数组法它是最简单的数组去重方法(indexOf方法)实现思路:新建一个数组,遍历去要重的数组,当值不在新数组的时候(indexOf为-1)就加入该......
  • 微服务是什么
    微服务(MicroServices)最初是由MartinFowler于2014年发表的论文《MicroServices》中提出的名词,它一经提出就成为了技术圈的热门话题。微服务,我们可以从字面上去理解,......
  • 周材系统115服务器莫名登录不上,迁移到150记录
    服务名  端口位置nacos  8848 /opt/nacos redis  6379 /opt/redis-5.0.5 zookeeper  2181 /opt/zooke......
  • 4.jenkins安装配置升级
    1.什么是jenkinsjenkins是一个开源提供友好操作界面的持续集成的工具,由java开发而成。jenkins是一个调度平台,本身不处理任何事情,都是借由插件完成所有的工作2.为什么使......
  • lua协程
    localcoroutine=coroutinelocaltable=tablelocalcoroutine_create=coroutine.createlocalcoroutine_resume=coroutine.resumelocalcoroutine_yield=co......
  • 包装类
    包装类前言:以前学集合的时候,在泛型的时候提到过基本数据类型需要使用他们的包装类,那到底包装类到底是什么呢,今天就详细聊聊什么是包装类,并且做几道关于包装类的题目。......
  • Docker 安装 Jenkins
    一、Docker介绍1、什么是dockerDocker容器是一个开源的应用容器引擎,让开发者可以以统一的方式打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何安装了docke......