开发教程

[JS]Date格式化

Foreve***Lv.3 中级会员 发表于 2024-6-7 · 最后回复 2024-10-13 IP:湖南省 2,082 2
Js中Date对象格式化
  1. //格式化主函数
  2. //date为时间对象
  3. function formatDate(date) {
  4.     return prefixInteger(date.getFullYear(), 4) + "-" + prefixInteger(parseInt(date.getMonth() + 1), 2) + "-" + prefixInteger(date.getDate(), 2);

  5. }
  6. //自动补零
  7. function prefixInteger(num, n) {

  8.     return (Array(n)
  9.         .join(0) + num)
  10.         .slice(-n);
  11. }
  12. console.log(formatDate(new Date))
复制代码
回复 2 最早 最新 只看楼主
您需要登录后才可以回帖 登录 | 立即注册
本版积分规则