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);
     
  • 相关阅读:
    filter与compress
    groupby,分组
    按照属性排序,使用lambda与itemgetter,attrgetter
    python3-cookbook电子书在线文档
    numpy中的向量操作
    向量Vector
    defaultdict与OrderedDict
    heapq堆队列
    (转载)SVM-基础(一)
    决策树-剪枝算法(二)
  • 原文地址:https://www.cnblogs.com/leise/p/14504302.html
Copyright © 2011-2022 走看看