JavaScript之Date类型(引用类型)

Date类型的创建

创建时间对象是由关键字new和构造函数Date()来创建的。

var d = new Date();

Date()中保存的是从1979年1月1日零时开始经过的毫秒数,再划分为年/月/日/时/分/秒/毫秒、周。

在调用Date()函数而不传入参数的情况下,将自动获得当前的日期和时间,

var d = new Date();

document.write(d); //Fri Mar 31 2017 21:48:49 GMT+0800 (中国标准时间)


Date.parse()方法

该方法接收一个表示日期的字符串参数,然后根据这个参数返回相应日期的 毫秒数

var date = new Date();
console.log(date);  //Thu Aug 17 2017 17:05:57 GMT+0800 (中国标准时间)

var date1 = new Date(Date.parse("May 25, 2017"));
console.log(date);  //Thu May 25 2017 00:00:00 GMT+0800 (中国标准时间)


Date.UTC()方法

该方法也是返回相应参数对应日期的毫秒数。不过,它接收的参数分别:年份、基于0的月份(0表示一月,11表示12月)、月中的天数(1到31)、时(0到23)、分(0到60)、毫秒数。

//表示2017年8月17日下午5点9分30秒
var data = new Date(Date.UTC(2017, 7, 17, 17, 09, 30));


Date.now()方法

该方法返回调用 这个方法时的毫秒数。

var data = new Date(Date.now()); //返回调用Date.now()这个方法时的日期/时间
var data1 = Date.now(); //返回调用这个方法时的毫秒数

console.log(data1); //1502961598607
console.log(data); //Thu Aug 17 2017 17:19:58 GMT+0800 (中国标准时间)


继承的方法

toString()

toString()可以将Date对象转换成字符串的形式并返回结果。


var d=new Date();
var n=d.toString();

console.log(n); //输出Fri Mar 31 2017 21:53:13 GMT+0800 (中国标准时间)


valueOf()

valueOf()返回Date对象的原始值。


var d=new Date();
var n=d.valueOf();

console.log(n); //1490968521547 原始值 从1970年1月1日零时到此时的毫秒数。


toLocaleString()


根据本地时间将Date对象转换成字符串。


var d=new Date();
var n=d.toLocaleString();

console.log(n); //输出Fri Mar 31 2017 21:53:13 GMT+0800 (中国标准时间)


日期格式化方法

toTimeString()

把Date对象的时间部分转换成字符串形式,以特定的格式显示时、分、秒、时区。

var d = new Date();
var n = d.toTimeString();

console.log(n); //22:01:12 GMT+0800 (中国标准时间)


toDateString()

把Date对象的日期部分转换成字符串形式,以特定的格式显示星期几、月、日、年。

var d = new Date();
var n = d.toDateString();

console.log(n); //Fri Mar 31 2017



日期/时间组件方法


getDate()--从Date对象中返回一个月中的某一天(1到31)


getDay()--从Date对象中返回一周中的某一天(0到6)0表示星期日,6表示星期六


getFullYear()--从Date对象中返回以四位数的年份(如:2017年)


getMonth()--从Date对象中返回月份(0到11)0表示一月,11表示12月


getHours()--从Date对象中返回小时(0到23时)


getMinutes()--从Date对象中返回分钟(0到60分钟)



"get-"是从Date对象中返回时间、日期的,而"set-"是自己设置的时间、日期。


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值