Date对象和Time对象
Date对象用于操作日期
Time对象用于操作时间
Date对象在使用时前需要先实例化,采用构造函数实例化。
var today=new Date();
// 返回年份
document.write(today.getFullYear());document.write(‘<br>‘);// 返回月份,从0到11,0表示1月,11表示12月document.write(today.getMonth());document.write(‘<br>‘);// 返回日期,具体某日document.write(today.getDate());document.write(‘<br>‘);// 返回星期几document.write(today.getDay());document.write(‘<br>‘);// 返回小时document.write(today.getHours());document.write(‘<br>‘);// 返回分document.write(today.getMinutes());document.write(‘<br>‘);// 返回秒document.write(today.getSeconds());document.write(‘<br>‘);// 返回毫秒,毫秒的范围从~999document.write(today.getMilliseconds());document.write(‘<br>‘);// 返回时间,这个时间是UTC开始计算的秒数,人看不懂document.write(today.getTime());document.write(‘<br>‘);// toTimeString 将时间转换成人看得懂的document.write(today.toTimeString(today.getTime()));document.write(‘<br>‘);var lastDate=new Date(‘1998-12-12T12:23:45‘);
document.write(lastDate.toDateString(lastDate.getTime()));document.write(‘<br>‘);document.write(lastDate.toTimeString(lastDate.getTime()));document.write(‘<br>‘);测试题目
1、如何创建Date对象?
答:
2、Date对象、Number对象、String对象、Math对象的区别是什么?
答:String 对象的属性和方法用于操作字符串。
Number对象通常用于操作数字。
Math对象用于数学上的计算,如算平方,四舍五入,生成随机数等等。
Date对象用于操作日期
3、如何使用Date对象生成一个指定的日期?
var lastDate=new Date(‘1998-12-12T12:23:45‘);
document.write(lastDate.toDateString(lastDate.getTime()));document.write(‘<br>‘);document.write(lastDate.toTimeString(lastDate.getTime()));document.write(‘<br>‘);4、如何计算两个日期之间的差距?
JavaScript&jQuery.Date对象和Time对象
原文:https://www.cnblogs.com/H97042/p/9160367.html