时间格式化,常用时间,格式

时间格式化,常用时间,格式 

const formatData = () => {
  let time, YYYYMMDDnorm, YYYYMMDD, YYYY, MM, MMChinese, DD, HHMMSS;
  let now = new Date();
  let year = now.getFullYear();       //年
  let month = now.getMonth() + 1;     //月
  let day = now.getDate();            //日
  let hh = now.getHours();            //时
  let mm = now.getMinutes();          //分
  let ss = now.getSeconds();           //秒

  time = year + "-";
  YYYYMMDDnorm = year + "-";
  HHMMSS = "";
  MMChinese = ""
  YYYY = year;
  if (month < 10) {
    time += "0";
    time += month + "-";
    YYYYMMDDnorm += "0";
    YYYYMMDDnorm += month + "-";
    MM = "0" + month;
  } else {
    time += month + "-";
    YYYYMMDDnorm += month + "-";
    MM = month;
  }
  if (day < 10) {
    time += "0";
    time += day + " ";
    YYYYMMDDnorm += "0";
    YYYYMMDDnorm += day;
    DD = "0" + day;
  } else {
    time += day + " ";
    YYYYMMDDnorm += day;
    DD = day;
  }
  if (hh < 10) {
    time += "0";
    time += hh + ":";
    HHMMSS += "0";
    HHMMSS += hh + ":";
  } else {
    time += hh + ":";
    HHMMSS += hh + ":";
  }
  if (mm < 10) {
    time += '0';
    time += mm + ":";
    HHMMSS += "0";
    HHMMSS += mm + ":";
  } else {
    time += mm + ":";
    HHMMSS += mm + ":";
  }
  if (ss < 10) {
    time += '0';
    time += ss;
    HHMMSS += "0";
    HHMMSS += ss;
  } else {
    time += ss;
    HHMMSS += ss;
  }
  switch (MM) {
    case "01":
      MMChinese = "一月"
      break
    case "02":
      MMChinese = "二月"
      break
    case "03":
      MMChinese = "三月"
      break
    case "04":
      MMChinese = "四月"
      break
    case "05":
      MMChinese = "五月"
      break
    case "06":
      MMChinese = "六月"
      break
    case "07":
      MMChinese = "七月"
      break
    case "08":
      MMChinese = "八月"
      break
    case "09":
      MMChinese = "九月"
      break
    case "10":
      MMChinese = "十月"
      break
    case "11":
      MMChinese = "十一"
      break
    case "12":
      MMChinese = "十二"
      break
    default:
      DDChinese = ""
  }

  YYYYMMDD = YYYYMMDDnorm.split('-').join('')
  return { time, YYYYMMDDnorm, YYYYMMDD, YYYY, MM, MMChinese, DD, HHMMSS }
}

const formatDataTime = (time) => {
  var timeF = (new Date(time.replace(/-/g, '/').valueOf()));
  let now = new Date(timeF);
  // console.log(new Date(strtime).getFullYear())
  // console.log(new Date(strtime).getMonth() + 1)
  // console.log(new Date(strtime).getDate())
  let year = now.getFullYear();       // 年
  let month = now.getMonth() + 1;     // 月
  let day = now.getDate();            // 日
  let hh = now.getHours();            // 时
  let mm = now.getMinutes();          // 分
  let ss = now.getSeconds();           // 秒

  if (month < 10) {
    month = "0" + month;
  } else {
    month = month;
  }
  if (day < 10) {
    day = "0" + day;
  } else {
    day = day;
  }
  if (hh < 10) {
    hh += "0";
  } else {
    hh = hh;
  }
  if (mm < 10) {
    mm += "0";
  } else {
    mm = mm;
  }
  if (ss < 10) {
    ss += "0";
  } else {
    ss = ss;
  }
  switch (month) {
    case "01":
      month = "一月"
      break
    case "02":
      month = "二月"
      break
    case "03":
      month = "三月"
      break
    case "04":
      month = "四月"
      break
    case "05":
      month = "五月"
      break
    case "06":
      month = "六月"
      break
    case "07":
      month = "七月"
      break
    case "08":
      month = "八月"
      break
    case "09":
      month = "九月"
      break
    case "10":
      month = "十月"
      break
    case "11":
      month = "十一"
      break
    case "12":
      month = "十二"
      break
    default:
      month = ""
  }

  return { year, month, day, hh, mm, ss}
}

export {
  formatData,
  formatDataTime
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值