美文网首页
时间格式化方式

时间格式化方式

作者: 女王陛下的喵 | 来源:发表于2019-03-22 15:07 被阅读0次
initDate(dateString) {
  const dateAll = new Date(parseInt(dateString) * 1000).toLocaleDateString()
  const dateArry = dateAll.split('/')
  const month = parseInt(dateArry[1])
  const day = parseInt(dateArry[2])
  const setMonth = month < 10 ? '0' + month : month
  const setDay = day < 10 ? '0' + day : day
  return dateArry[0] + '-' + setMonth + '-' + setDay
}
/**
 * 时间格式化
 * @param {*} time  时间戳
 * @param {*} cFormat 要显示的格式 {y}-{m}-{d}
 * @return {string}   格式化的时间字符串
 */
formatterTime(time, cFormat) {
  if (arguments.length === 0) return null
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  if ((typeof time === 'number') && (time.toString().length === 10)) time = time * 1000
  const 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() // 周几, 星期天的时候为0
  }
  return format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') { return `星期${['日', '一', '二', '三', '四', '五', '六'][value]}` }
    if (result.length > 0 && value < 10) value = '0' + value
    return value || 0
  })
}

相关文章

  • java DateFormat

    DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。日期/时间格式化...

  • moment格式化时间

    nodejs中的moment模块可以格式化时间使用: //默认格式化当前时间两种方式

  • 时间格式化方式

  • Go语言标准库之time

    Go语言标准库之time 时间的格式化和解析 格式化 FormatGo语言和其他语言的时间格式化的方式不同,Go语...

  • Go语言标准库之time

    Go语言标准库之time 时间的格式化和解析 格式化 Format Go语言和其他语言的时间格式化的方式不同,Go...

  • 时间转换函数moment.js

    moment.js是一款转换时间的插件 安装方式为: 其常用的格式化参数如下表所示: 常用的转换 1.时间格式化 ...

  • 【Python扫盲】字符串格式化

    字符串格式化 Python的字符串格式化有两种方式 %格式符方式 format方式 事实上,格式化借助的是对象的魔...

  • 量化交易入门笔记-Datetime和Time模块

    Python 中,通常有三种方式用来表示时间,分别是时间戳、格式化的字符串、元组(struct_time)方式 时...

  • Go语言开发时间格式化

    习惯了IOS时间格式化的方式,在go语言开发的时候,在Go语言开发的时候竟然为格式化时间还查了半天资料,看完资料之...

  • 优雅的时间格式化方式

网友评论

      本文标题:时间格式化方式

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