一、date对象
javascript中内置了一个date对象,它非常强大,提供了很多属性和方法,用来创建、操作、格式化日期和时间,也可以进行各种日期和时间的相关操作;
1、创建日期
使用new date() 构造函数来创建date实例对象;
(1)常用方法
序号 | 方法 | 说明 |
---|---|---|
1 | new date() | 根据当前时间创建date对象; |
2 | new date(milliseconds) | 根据milliseconds(13位时间戳)创建date对象; |
3 | new date(datestring) | 根据datestring(如:"2024/2/20 11:11:00")日期字符串创建date对象; |
4 | new date(year, month [, day, hours, minutes, seconds, milliseconds] ) | 根据传入的年,月,日,时,分,秒,毫秒创建,前两个参数必选; |
(2)使用示例
// 1、不传参数,根据当前日期创建 console.log("根据当前日期创建:", new date()); // 2、传入时间戳创建 console.log("根据时间戳创建:", new date(1719363544868)); // 3、传入日期字符串创建 console.log("根据日期字符串:", new date("2014/01/08 11:11:00")); // 4、传入年,月,日,时,分,秒,毫秒创建 console.log("根据年,月,日,时,分,秒,毫秒创建:", new date(2014, 2, 20, 11, 30, 23));
2、获取日期
(1)常用方法
序号 | 方法 | 描述 |
---|---|---|
1 | getfullyear() | 获取4位数年份 |
2 | getmonth() | 获取月份(0-11,0代表一月) |
3 | getdate() | 获取月份中的某一天(1-31) |
4 | gethours() | 获取小时数(0-23) |
5 | getminutes() | 获取分钟数(0-59) |
6 | getseconds() | 获取秒数(0-59) |
7 | getmilliseconds() | 获取毫秒数(0-999) |
8 | gettime() | 获取自1970年1月1日以来的毫秒数 |
9 | getday() | 获取星期中的某一天(0-6,0代表周日) |
(2)使用示例
// 创建当前时间的日期对象 const date = new date("2014-01-08 14:05:06"); console.log("新创建的date对象:", date); // 1、获取4位数年份 console.log("getfullyear()方法,获取4位数年份:", date.getfullyear()); // 2、获取月份(0-11,0代表一月) console.log("getmonth()方法,获取月份:", date.getmonth() + 1); // 3、获取月份中的某一天(1-31) console.log("getdate()方法,获取月份中的某一天:", date.getdate()); // 4、获取小时数(0-23) console.log("gethours()方法,获取小时数:", date.gethours()); // 5、获取分钟数(0-59) console.log("getminutes()方法,获取分钟数:", date.getminutes()); // 6、获取秒数(0-59) console.log("getseconds()方法,获取秒数:", date.getseconds()); // 7、获取毫秒数(0-999) console.log("getmilliseconds()方法,获取毫秒数:", date.getmilliseconds()); // 8、获取自1970年1月1日以来的毫秒数 console.log("gettime()方法,获取自1970年1月1日以来的毫秒数:", date.gettime()); // 9、获取星期中的某一天(0-6,0代表周日) console.log("getday()方法,获取星期中的某一天:", date.getday());
3、设置日期
(1)常用方法
序号 | 方法 | 描述 |
---|---|---|
1 | setfullyear() | 设置4位数年份 |
2 | setmonth() | 设置月份(0-11,0代表一月) |
3 | setdate() | 设置月份中的某一天(1-31) |
4 | sethours() | 设置小时数(0-23) |
5 | setminutes() | 设置分钟数(0-59) |
6 | setseconds() | 设置秒数(0-59) |
7 | setmilliseconds() | 设置毫秒数(0-999) |
(2)使用示例
let date = new date(); console.log("新创建的date对象:", date); // 1、设置4位数年份 date.setfullyear(2020); console.log("setfullyear()方法,设置4位数年份:", date); // 2、设置月份(0-11,0代表一月) date.setmonth(10); console.log("setmonth()方法,设置月份:", date); // 3、设置月份中的某一天(1-31) date.setdate(7); console.log("setdate()方法,设置月份中的某一天:", date); // 4、设置小时数(0-23) date.sethours(12); console.log("sethours()方法,设置小时数:", date); // 5、设置分钟数(0-59) date.setminutes(24); console.log("setminutes()方法,设置分钟数:", date); // 6、设置秒数(0-59) date.setseconds(48); console.log("setseconds()方法,设置秒数:", date); // 7、设置毫秒数(0-999) date.setmilliseconds(123); console.log("setmilliseconds()方法,设置毫秒数:", date); console.log("设置完成后的日期:", date);
4、转换日期
(1)常用方法
序号 | 方法 | 描述 |
---|---|---|
1 | tostring() | 把 date 对象转换为字符串; |
2 | todatestring() | 把 date 对象的日期部分转换为字符串; |
3 | totimestring() | 把 date 对象的时间部分转换为字符串; |
4 | tolocalestring() | 把 date 对象,转换为本地时间格式的字符串; |
5 | tolocaledatestring() | 把 date 对象的日期部分,转换为本地时间格式的字符串; |
6 | tolocaletimestring() | 把 date 对象的时间部分,转换为本地时间格式的字符串; |
7 | tojson() | 把 date 对象转换为json 数据格式字符串; |
8 | toisostring() | 把 date 对象转换为 iso 标准的日期格式; |
(2)使用示例
let date = new date("2014-01-08 14:05:06"); console.log("新创建的date对象:", date); // 1、把 date 对象转换为字符串; console.log("tostring()方法,转换为字符串:", date.tostring()); // 2、把 date 对象的日期部分转换为字符串; console.log("todatestring()方法,日期部分转换为字符串:", date.todatestring()); // 3、把 date 对象的时间部分转换为字符串; console.log("totimestring()方法,时间部分转换为字符串:", date.totimestring()); // 4、把 date 对象,转换为本地时间格式的字符串; console.log("tolocalestring()方法,转换为本地时间格式的字符串:", date.tolocalestring()); // 5、把 date 对象的日期部分,转换为本地时间格式的字符串; console.log("tolocaledatestring()方法,日期部分转换为本地时间格式的字符串:", date.tolocaledatestring()); // 6、把 date 对象的时间部分,转换为本地时间格式的字符串; console.log("tolocaletimestring()方法,时间部分,转换为本地时间格式的字符串:", date.tolocaletimestring()); // 7、把 date 对象转换为json 数据格式字符串; console.log("tojson()方法,转换为json 数据格式字符串:", date.tojson()); // 8、把 date 对象转换为 iso 标准的日期格式; console.log("toisostring()方法,转换为 iso 标准的日期格式:", date.toisostring());
5、获取时间戳
(1)常用方法
序号 | 方法 | 说明 |
---|---|---|
1 | date.parse(new date()) | 13位,精确到秒; |
2 | math.round(new date()) | 13位,精确到毫秒; |
3 | (new date()).valueof() | 13位,精确到毫秒; |
4 | new date().gettime() | 13位,精确到毫秒; |
5 | +new date() | 13位,精确到毫秒; |
(2)使用示例
// 1、使用date.parse() console.log("使用date.parse()方法获取时间戳:", date.parse(new date())); // 2、使用math.round() console.log("使用math.round()方法获取时间戳:", math.round(new date())); // 3、使用.valueof()() console.log("使用.valueof()方法获取时间戳:", (new date()).valueof()); // 4、使用.gettime() console.log("使用.gettime()方法获取时间戳:", (new date()).gettime()); // 5、使用+new date() console.log("使用+new date()方法获取时间戳:", +new date());
二、格式化date对象
不难发现,使用new date()创建出来的日期对象,格式并不是想要的;
下面封装格式化日期的方法,供日常开发参考;
// 创建当前时间的日期对象 const date = new date("2014-01-08 14:05:06"); console.log("新创建的date对象:", date); console.log(format1(date)); console.log(format2(date)); console.log(format3(date)); console.log(format4(date)); console.log(format5(date)); console.log(format6(date)); console.log(format7(date)); // 1、输出日期:y:m:d(不补0) function format1(date){ const year = date.getfullyear(); const month = date.getmonth() + 1; const day = date.getdate(); return year + '-' + month + '-' + day; } // 2、输出日期:yyyy-mm-dd(补0) function format2(date){ let year = date.getfullyear(); let month = date.getmonth() + 1; let day = date.getdate(); month = month < 10 ? "0" + month : month; day = day < 10 ? "0" + day : day; return year + '-' + month + '-' + day; } // 3、输出时间:h:m:s function format3(date){ const hour = date.gethours(); const minute = date.getminutes(); const second = date.getseconds(); return hour + ':' + minute + ':' + second; } // 4、输出时间:hh:mm:ss function format4(date){ let hour = date.gethours(); let minute = date.getminutes(); let second = date.getseconds(); hour = hour < 10 ? "0" + hour : hour; minute = minute < 10 ? "0" + minute : minute; second = second < 10 ? "0" + second : second; return hour + ':' + minute + ':' + second; } // 5、输出完整日期:y-m-d h:m:s function format5(date){ const year = date.getfullyear(); const month = date.getmonth() + 1; const day = date.getdate(); const hour = date.gethours(); const minute = date.getminutes(); const second = date.getseconds(); return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; } // 6、输出完整日期:yy-mm-dd hh:mm:ss function format6(date){ let year = date.getfullyear(); let month = date.getmonth() + 1; let day = date.getdate(); let hour = date.gethours(); let minute = date.getminutes(); let second = date.getseconds(); month = month < 10 ? "0" + month : month; day = day < 10 ? "0" + day : day; hour = hour < 10 ? "0" + hour : hour; minute = minute < 10 ? "0" + minute : minute; second = second < 10 ? "0" + second : second; return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; } // 7、输出完整日期:yy-mm-dd hh:mm:ss week function format7(date){ let year = date.getfullyear(); let month = date.getmonth() + 1; let day = date.getdate(); let hour = date.gethours(); let minute = date.getminutes(); let second = date.getseconds(); let week = ['日', '一', '二', '三', '四', '五', '六'][date.getday()]; month = month < 10 ? "0" + month : month; day = day < 10 ? "0" + day : day; hour = hour < 10 ? "0" + hour : hour; minute = minute < 10 ? "0" + minute : minute; second = second < 10 ? "0" + second : second; return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + ' 星期' + week; }
到此这篇关于javascript中的 date(日期)对象的文章就介绍到这了,更多相关js date对象内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论