当前位置: 代码网 > it编程>编程语言>Javascript > RFC3339时间格式与普通时间格式函数相互转换

RFC3339时间格式与普通时间格式函数相互转换

2024年07月28日 Javascript 我要评论
RFC3339时间格式与普通时间格式,相互转换函数封装
// untils.js
/**
 * rfc时间格式 转换为 普通时间格式
 * @param {*} date
 * @returns
 */
export const rfctodate = (date) => {
  const date1 = new date(date).tojson();
  const newdate = new date(+new date(date1) + 8 * 3600 * 1000)
    .toisostring()
    .replace(/t/g, ' ')
    .replace(/\.[\d]{3}z/, '');
  return newdate;
};

/**
 * 普通时间格式 转换为 rfc时间格式
 * @param {*} date
 * @returns
 */
export const datetorfc = (date) => {
  const y = new date(date).getfullyear();
  const m =
    new date(date).getmonth() + 1 < 10
      ? `0${new date(date).getmonth() + 1}`
      : new date().getmonth() + 1;
  const d =
    new date(date).getdate() < 10
      ? `0${new date(date).getdate()}`
      : new date(date).getdate();
  const hh =
    new date(date).gethours() < 10
      ? `0${new date(date).gethours()}`
      : new date(date).gethours();
  const mm =
    new date(date).getminutes() < 10
      ? `0${new date(date).getminutes()}`
      : new date(date).getminutes();
  const ss =
    new date(date).getseconds() < 10
      ? `0${new date(date).getseconds()}`
      : new date(date).getseconds();
  let enddate = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
  enddate = `${enddate.replace(/\s+/g, 't')}+08:00`;
  return enddate;
};

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com