Chinaunix首页 | 论坛 | 博客
  • 博客访问: 564353
  • 博文数量: 208
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2049
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-19 21:54
文章分类

全部博文(208)

文章存档

2019年(18)

2018年(27)

2017年(5)

2016年(53)

2015年(88)

2014年(17)

分类: JavaScript

2019-04-16 11:13:28

const formatDate = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return [year, month, day].map(formatNumber).join('-');
}
const formatDate_cn = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return month + '月' + day + '日';
}

const formatTime = function (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()
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatTime_interval = function (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()

  return [year, month, day].map(formatNumber).join('/') + '   ' + [hour, minute].map(formatNumber).join(':')
}


const formatTime_intervalTime = function (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()

  return [hour, minute].map(formatNumber).join(':')
}


const formatTime_cn = date => {
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  var today = new Date().getDate()
  var dayText = formatDate(date)
  if (day == today) {
    dayText = '今天';
  } else if (day == today - 1) {
    dayText = '昨天';
  } else if (day == today - 2) {
    dayText = '前天';
  }
  return (dayText + ' ' + [hour, minute].map(formatNumber).join(':'))
}

const timetransform = time => {
  var seconds = numtransform(time % 60)
  var minutes = numtransform(Math.floor(time / 60))
  var hours = numtransform(Math.floor(time / 60 / 60))
  return ((hours > 0 ? hours + ':' : '') + minutes + ":" + seconds)
}

//时间转换
const numtransform = num => {
  if (num < 10) {
    return "0" + num
  } else {
    return num
  }
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
module.exports = {
  formatDate: formatDate,  //new Date(time) 转换成2018-11-11
  formatDate_cn: formatDate_cn,//new Date(time) 转换成2018年11月11日
  formatTime: formatTime,//new Date(time) 转换成2018-11-11 11:11:11
  formatTime_interval: formatTime_interval,//new Date(time) 转换成2018/11/11 11:11
  timetransform: timetransform,     //113秒转00:00:00
  formatTime_cn: formatTime_cn,   //new Date(time)2018-12-12T09:26:28.000Z   获取今天/昨天/前天/日期+13:33
  formatTime_intervalTime: formatTime_intervalTime,   //转换成00:00 时间
 
}
阅读(709) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册