时间

// time 01~09
export function addZero(time: number) {
  return time > 10 ? time.toString() : "0" + time;
}
// zone
export function Zone(zone: number) {
  return zone > 0 ? "-" + Math.abs(zone) : "+" + Math.abs(zone);
}
// initial format 2019-08-08 12:50:09
export function timeFormat(date?: any, format?: any) {
  const time = date;
  const year = time.getFullYear();
  const month = addZero(time.getMonth() + 1);
  const day = addZero(time.getDate());
  const hours = addZero(time.getHours());
  const minute = addZero(time.getMinutes());
  const second = addZero(time.getSeconds());
  const milliSecond = addZero(time.getMilliseconds());
  const zone = time.getTimezoneOffset() / 60;
  const newZone = Zone(zone);
 let dateWithFormat: any;
  if (format === "YYYY") {
    dateWithFormat = year + "";
  } else if (format === "YYYY-MM") {
    dateWithFormat = year + "-" + month;
  } else if (format === "YYYY-MM-DD") {
    dateWithFormat = year + "-" + month + "-" + day;
  } else if (format === "YYYY-MM-DDThh:mmTZD") {
    dateWithFormat = year + "-" + month + "-" + day + "T" + hours + ":" + minute + newZone + ":00";
 } else if (format === "YYYY-MM-DDThh:mm:ssTZD") {
    dateWithFormat = year + "-" + month + "-" + day + "T" + hours + ":" + minute + ":" + second + newZone + ":00";
} else if (format === "YYYY-MM-DDThh:mm:ss.sTZD") {
    dateWithFormat = year + "-" + month + "-" + day + "T" + hours + ":" + minute + ":" + second + ":" + milliSecond
      + newZone + ":00";
  }
  return dateWithFormat;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值