JavaScript Date valueOf()方法

下面是一个例子 日期:() 方法

null
  • 例子:

    javascript

    <script>
    // Here a date has been assigned
    // while creating Date object
    var dateobj =
    new Date( 'October 15, 1996 05:35:32' );
    // Getting the number of milliseconds between
    // 1 January 1970 00:00:00
    // UTC and the given date as the content of
    // the above Date() constructor.
    var B = dateobj.valueOf();
    // Printing the calculated number
    // of milliseconds.
    document.write(B);
    </script>

    
    

  • 输出:
    845337932000

这个 日期价值 方法用于获取1970年1月1日00:00:00 UTC和给定日期之间的毫秒数。 语法:

dateObj.valueOf()

参数: 此方法不接受任何参数。它只是与使用Date()构造函数创建的日期对象一起使用。

返回值: 它返回1970年1月1日00:00:00 UTC和给定日期之间的毫秒数,作为date()构造函数的内容。

注: 这个 DateObj 是使用Date()构造函数创建的有效日期对象,其内容用于获取1970年1月1日00:00:00 UTC和给定日期之间的毫秒数作为Date()构造函数的内容。

以上方法的更多代码如下:

项目1: 如果在创建日期对象时未传递任何参数,但valueOf()方法返回1970年1月1日00:00:00 UTC和当前日期之间的毫秒数。

javascript

<script>
// Here nothing has been assigned
// while creating Date object
var dateobj = new Date();
// Getting the number of milliseconds between
// 1 January 1970 00:00:00
// UTC and the current date.
var B = dateobj.valueOf();
// Printing the calculated number
// of milliseconds.
document.write(B);
</script>


输出:

1524387231290

项目2: 一个月的日期介于1到31之间。如果日期为35,超出了日期范围,则返回NaN,即不是数字。

javascript

<script>
// Here a date has been assigned
// while creating Date object
var dateobj =
new Date( 'October 35, 1996 05:35:32' );
// Getting the number of milliseconds between
// 1 January 1970 00:00:00
// UTC and the given date.
var B = dateobj.valueOf();
// Printing the calculated number
// of milliseconds.
document.write(B);
</script>


输出:

NaN

一些要点:

  • 月份、日期、小时、分钟、秒、毫秒都应该在各自的范围内。否则valueOf()方法返回NaN,即不是数字。
  • 月份、日期、小时、分钟、秒和毫秒的范围分别为0到11、1到31、0到23、0到59、0到59、0到999。

支持的浏览器: 支持的浏览器 JavaScript Date valueOf()方法 以下列出了:

  • 谷歌Chrome 1及以上版本
  • 边缘12及以上
  • Firefox 1及以上版本
  • Internet Explorer 4及以上版本
  • 歌剧3及以上
  • Safari 1及以上
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享