当前位置: 移动技术网 > IT编程>网页制作>CSS > 时间格式化解析

时间格式化解析

2018年12月06日  | 移动技术网IT编程  | 我要评论

时间格式化

自己总结的一些时间格式化,可能比较娄,但是还挺好用的

//日期格式化2018-9-9变成2018-09-09与前几天日期
export function getday(num, str) {
  let today = new date();
  let nowtime = today.gettime();
  let ms = 24 * 3600 * 1000 * num;
  today.settime(parseint(nowtime + ms));
  let oyear = today.getfullyear();
  let omoth = (today.getmonth() + 1).tostring();
  if (omoth.length <= 1) omoth = '0' + omoth;
  let oday = today.getdate().tostring();
  if (oday.length <= 1) oday = '0' + oday;
  return oyear + str + omoth + str + oday;
}


//时间补0
export function fillzero(num) {
  if (parseint(num) < 10) {
    num = '0' + num;
  }
  return num;
}

//时间戳转换13位数
export function transdate(str) {
  let odate = new date(str),
    oyear = odate.getfullyear(),
    omonth = odate.getmonth() + 1,
    oday = odate.getdate(),
    ohour = odate.gethours(),
    minutes = odate.getminutes(),
    otime = fillzero(oyear) + '-' + fillzero(omonth) + '-' + fillzero(oday) + ' ' + fillzero(ohour) + ':' + fillzero(minutes) //最后拼接时间
  return otime;
}

// 时间转换为2018-03-09 10:20:00 
export function transdatetime(str) {
  let odate = new date(str),
    oyear = odate.getfullyear(),
    omonth = odate.getmonth() + 1,
    oday = odate.getdate(),
    ohour = odate.gethours(),
    minutes = odate.getminutes(),
    second = odate.getseconds(),
    otime = fillzero(oyear) + '-' + fillzero(omonth) + '-' + fillzero(oday) + ' ' + fillzero(ohour) + ':' + fillzero(minutes) + ':' + fillzero(second) //最后拼接时间
  return otime;
}

//时间戳转换10位数
export function transdates(str) {
  let odate = new date(str * 1000),
    oyear = odate.getfullyear(),
    omonth = odate.getmonth() + 1,
    oday = odate.getdate(),
    otime = fillzero(oyear) + '年' + fillzero(omonth) + '月' + fillzero(oday) + "日"; //最后拼接时间
  return otime;
}

//时间戳模板2018-09-08
export function datetemp(str) {
  let odate = new date(str),
    oyear = odate.getfullyear(),
    omonth = odate.getmonth() + 1,
    oday = odate.getdate(),
    otime = fillzero(oyear) + '-' + fillzero(omonth) + '-' + fillzero(oday); //最后拼接时间
  return otime;
}

//时间戳模板2018.09.08
export function datesave(str) {
  let odate = new date(str),
    oyear = odate.getfullyear(),
    omonth = odate.getmonth() + 1,
    oday = odate.getdate(),
    otime = fillzero(oyear) + '.' + fillzero(omonth) + '.' + fillzero(oday); //最后拼接时间
  return otime;
}

模板也许都差不多 还能还有其他情况我没有写上去
调用的方法

//就假设这几种吧  其他的自己可以试试看
datesave(new date())
transdatetime(new date())
datetemp(new date())

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网