为什么新的Date()。setHours(18)会返回一个数字而不是一个日期? [英] Why does new Date().setHours(18) return a number instead of a date?

查看:213
本文介绍了为什么新的Date()。setHours(18)会返回一个数字而不是一个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么第一行代码返回Date类型的对象,而第二行返回number?



根据doc( http://www.w3schools.com/jsref/jsref_sethours.asp )setHours()应该只需更改日期对象的小时值,而不是转换它。



代码:

  var date = new Date(); 

var date2 = date.setHours(19);

谢谢!

解决方案

setHours()方法根据本地时间设置指定日期的小时数,并返回自1970年1月1日00:00:00 UTC以来直到更新的Date实例所代表的时间的毫秒数。 / p>

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours


Why do the first line of code return an object of type "Date" whereas the second one returns a "number" ?

According to the doc (http://www.w3schools.com/jsref/jsref_sethours.asp) setHours() should just change the value of the hour of the date object, not convert it.

The code :

  var date = new Date();

  var date2 =date.setHours(19);

Thanks !

解决方案

The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds since 1 January 1970 00:00:00 UTC until the time represented by the updated Date instance.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours

这篇关于为什么新的Date()。setHours(18)会返回一个数字而不是一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆