时间日期对象创建
//当前时间的时间日期对象
var date =new Date()
//指定的时间日期对象
var date = new Date("年-月-日 时:分:秒") // 也可以是英文版的时间字符串 - Sun May 13,2016
var date = new Date(年,月,日,时,分,秒)
var date = new Date(时间戳)
2.时间戳的获取(三种方法)
Date.parse('2025-2-23')// 获取1970年到设定时间的毫秒数
new Date().getTime()
+new Date()
3.获取具体的时间日期
date.getFullYear(); // 获取到完整的时间日期对象中的年份
date.getMonth(); // 获取到时间日期对象中的月份 - 这里的月份是通过0~11来描述1~12月的
date.getDate(); // 获取到时间日期对象中的日
date.getDay(); // 获取时间日期对象中的星期几
date.getHours(); // 获取到时间日期对象中的时
date.getMinutes(); // 获取到时间日期对象中分
date.getSeconds(); // 获取到时间日期对象中的秒
date.getMilliseconds(); // 获取到时间日期对象中的毫秒 - 1秒=1000毫秒
date.getTime(); // 获取到时间日期对象对应的时间戳
4.设置时间日期
date.setFullYear(年份); // 设置时间日期对象中的年份
date.setMonth(当前月份-1); // 设置时间日期对象中的月份 - 这里的月份是通过0~11来描述1~12月的
date.setDate(日); // 设置时间日期对象中的日
date.setHours(时); // 设置时间日期对象中的时
date.setMinutes(分); // 设置时间日期对象中分
date.setSeconds(秒); // 设置时间日期对象中的秒
date.setMilliseconds(毫秒); // 设置时间日期对象中的毫秒
date.setTime(时间戳); // 设置时间日期对象对应的时间戳 - 直接用时间戳就能将时间日期对象中的年月日时分秒全部改变
5.日期格式化
date.toLocalString();//本地风格的日期格式
date.toLocaleDateString(); // 获取日期
date.toLocaleTimeString(); // 获取时间