js获取当前时间、年月日、星期几

复制代码
  let date = new Date()

  // 获取时间:时分秒
  const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
  const minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
  const secound = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
  this.datetime = hour + ':' + minute + ':' + secound

  // 获取日期:年月日
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  this.nowDate = month + "" + day + ""
  this.nowYear = year + ""

  // 获取星期几
  const weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  this.nowWeek = weeks[new Date().getDay()];
复制代码

废话不多说,直接上代码。

只有及早为自己的前程努力,世界才会早日给你惊喜。只有缩短成功的时日,才能延长生活享受的时间。

posted @   人渴浸思茶  阅读(772)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示