zoukankan      html  css  js  c++  java
  • 日期时间格式或封装,已经互相转换,抽出来日后方便自己开发,之前用作在mpvue的框架开发小程序中

    // 时间格式化
    export const dateFormat = (date, string) => {
      date = new Date(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()
      if (string == "YYYY年") {
        return [year].map(formatNumber).join('年') + '年'
      } else if (string == "YYYY/MM/DD") {
        return [year, month, day].map(formatNumber).join('/')
      } else if (string == "YYYY/MM/DD HH:MM") {
        return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':')
      } else if (string == "YYYY.MM.DD HH:MM") {
        return [year, month, day].map(formatNumber).join('.') + ' ' + [hour, minute].map(formatNumber).join(':')
      } else if (string == "MM/DD") {
        return [month, day].map(formatNumber).join('/')
      } else if (string == "MM月DD日") {
        return [month, day].map(formatNumber).join('月') + '日 '
      } else if (string == "MM月DD日 HH:MM") {
        return [month, day].map(formatNumber).join('月') + '日 ' + [hour, minute].map(formatNumber).join(':')
      } else if (string == "MM-DD HH:MM") {
        return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
      } else if (string == "YYYY-MM-DD") {
        return [year, month, day].map(formatNumber).join('-')
      } else if (string == "YYYY.MM.DD") {
        return [year, month, day].map(formatNumber).join('.')
      } else if (string == "YYYY年MM月DD日 HH:MM") {
        return [year].map(formatNumber).join('年') + '年' + [month, day].map(formatNumber).join('月') + '日 ' + [hour, minute].map(formatNumber).join(':')
      } else if (string == "YYYY年MM月DD日") {
        return [year].map(formatNumber).join('年') + '年' + [month, day].map(formatNumber).join('月') + '日 '
      } else if (string == "HH:MM") {
        return [hour, minute].map(formatNumber).join(':')
      } else if (string == "HH:MM:SS") {
        return [hour, minute, second].map(formatNumber).join(':')
      } else {
        return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
      }
    }
    const formatNumber = n => {
    // 主要用作判断是否在个位数的时候 前面添加一个0 n
    = n.toString() return n[1] ? n : '0' + n } // 日期格式转换成时间戳 export const timeToTimestamp = (date, time) => { let dateStr = new Date(date + ' ' + time) return dateStr.getTime() }

    想要用的时候 复制粘贴就好了

  • 相关阅读:
    Mac 终端命令使用自动补全时忽略大小写设置
    Android App专项测试
    评估产品机会
    如何快速获取ListView的打气筒对象
    js处理日期格式yyyy-MM-dd hh:mm:ss
    websocket聊天时,图片压缩处理(url或者input-file)
    canvas图片压缩,局部放大,像素处理
    vscode 右键文件或者文件夹显示菜单
    HTML5-SQLLite连接
    ie下div模拟的表格,表头表体无法对齐
  • 原文地址:https://www.cnblogs.com/PinkYun/p/10548643.html
Copyright © 2011-2022 走看看