JS中日期对象Date(易懂)
让我为大家介绍一下日期对象吧!
日期对象,用来表示时间的对象
1.获取当前时间
// 获取当前时间
let date = new Date()
2.指定时间
// 指定时间
let date = new Date("2023-6-1 08:30:30")
日期对象的方法
因为我们日期对象返回的数据我们不能直接使用,所以需要转换为实际开发中常用的格式
方法 | 作用 | 说明 |
---|---|---|
getFullYear() | 获取年份 | 获取四位年份 |
getMonth() | 获取月份 | 取值 0 ~ 11 |
getDate() | 获取月份中的每一天 | 不同月份取值也不相同 |
getDay() | 获取星期 | 取值 0 ~ 6 |
getHours() | 获取小时 | 取值 0 ~ 23 |
getMinutes() | 获取分钟 | 取值 0 ~ 59 |
getSeconds() | 获取秒 | 取值 0 ~ 59 |
// 获取当前时间
let date = new Date()
// 使用方法
// 获取年份
console.log(date.getFullYear())
// 获取月份
console.log(date.getMonth() + 1)
// 获取日期
console.log(date.getDate())
// 获取星期
console.log(date.getDay())
// 获取小时
console.log(date.getHours())
// 获取分钟
console.log(date.getMinutes())
// 获取秒
console.log(date.getSeconds())
时间戳
获取时间戳的三种方法
1.getTime()
2. +new Date()
3. Date.now()
let date = new Date()
// 1.getTime()
console.log(date.getTime())
// 2. +new Date()
console.log(+new Date)
// 3. Date.now
console.log(Date.now())
感谢大家的阅读,本人文笔有限,如有不对的地方,可以向我指出,感谢大家!
分类:
JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南