2025年3月24日 星期一 甲辰(龙)年 月廿三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > JavaScript

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

时间:05-18来源:作者:点击数:31

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()];
效果如下:
在这里插入图片描述
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门