美文网首页
格式化年月日时分秒

格式化年月日时分秒

作者: 撑船的摆渡人 | 来源:发表于2019-10-21 10:58 被阅读0次
/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time, 10) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  return format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
}

相关文章

  • 4.时间的处理,monent插件

    1.格式化时间 2.格式化年月日时分秒 3.通过获取当前时间的时间戳转化为年月日时分秒 4.某个时间时间戳转换成日...

  • js 时间格式化

    dateTime=====>要格式化的数据(包括:时间戳,年月日时分秒...) fmt===>要格式化的格式(y:...

  • js业务常用函数

    js来控制 字符串的长度! 去掉年月日时分秒 里的 时分秒! 根据时间差值 输出 倒计时的格式化 更加细则的判断数...

  • 13 Golang time包以及日期相关函数

    打印当前日期 注意:%02d中的2表示宽度,如果整数不够2列就补上0 年月日-时分秒对应格式化模板 年月日时(12...

  • 系统时间相关-date

    1.查看系统时间 年月日 完整显示年月日 完整显示年月日 时分秒 时分秒 星期 显示过去或未来的时间信息: 同步系...

  • Day19-课堂笔记-time

    主要包含处理年月日时分秒对应的时间(着重时分秒)import time专门处理年月日import datetime...

  • datetime:时间戳转换

    将时间戳转换为年月日时分秒格式: 将年月日时分秒格式转换为时间戳:

  • Date类,DateFormat类,Calendar类

    Date类 日期和毫秒值的相互转换 简单是件推算时间 年月日时分秒 DateFormat 是日期/时间格式化子类的...

  • day19

    一、time模块 主要包含处理年月日时分秒对应的事件(着重时分秒)import time 专门处理年月日impo...

  • day19-time模块

    一、time模块 主要包含处理年月日时分秒对应的事件(着重时分秒)import time 专门处理年月日impo...

网友评论

      本文标题:格式化年月日时分秒

      本文链接:https://www.haomeiwen.com/subject/phzzmctx.html