为什么新的Date()。setHours(18)会返回一个数字而不是一个日期?

编程入门 行业动态 更新时间:2024-10-15 12:37:06
本文介绍了为什么新的Date()。setHours(18)会返回一个数字而不是一个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

代码:

var date = new Date(); var date2 = date.setHours(19);

谢谢!

解决方案

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

developer.mozilla/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 (www.w3schools/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.

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

更多推荐

为什么新的Date()。setHours(18)会返回一个数字而不是一个日期?

本文发布于:2023-10-24 21:14:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   而不   日期   数字   Date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!