zoukankan      html  css  js  c++  java
  • 获取各维度下的开始时间并格式化

    最近开发需要,用js获得当日、本月、当年的开始时间,并进行格式化为
    yyyy-MM-dd hh:mm:ss
    /**
     *格式化时间
     *yyyy-MM-dd hh:mm:ss
     */
    formatDate(time, fmt) {
      if (time === undefined || '') {
        return
      }
      const date = new Date(time)
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      }
      for (const k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          const str = o[k] + ''
          fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
        }
      }
      return fmt
    }
        const now = new Date();
          const fmt = 'yyyy-MM-dd 00:00:00';
          // 当日开始时间
          const dayStart = formatDate(now, fmt);
          // 当前时间
          const dayNow = formatDate(now, 'yyyy-MM-dd hh:mm:ss');
          // 当月开始时间
          const m = now.setDate(1);
          const monthStart = formatDate(m, fmt);
          // 当年开始时间
          const y = now.setMonth(0);
          const yearStart = formatDate(y, fmt);
          console.log(dayStart);
          console.log(dayNow);
          console.log(monthStart);
          console.log(yearStart);
     
  • 相关阅读:
    Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 1
    架构-MVVM:MVVM核心概念
    架构-MVVC:百科
    架构:目录
    架构:template
    JavaScript-Tool:Ext JS
    JavaScript-Tool:jquery.tree.js-un
    JavaScript-Tool:wdtree
    C#:C# 运算符
    C#:目录
  • 原文地址:https://www.cnblogs.com/leise/p/14504302.html
Copyright © 2011-2022 走看看