JavaScript&jQuery.Date对象和Time对象

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>');
// 返回毫秒,毫秒的范围从~999
document.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对象?

答:

  1. new Date(“month dd,yyyy hh:mm:ss”);
  2. new Date(“month dd,yyyy”);
  3. new Date(yyyy,mth,dd,hh,mm,ss);
  4. new Date(yyyy,mth,dd);
  5. new Date(ms);

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、如何计算两个日期之间的差距?

猜你喜欢

转载自www.cnblogs.com/H97042/p/9160367.html