JavaScript如何使用Date now()方法?

2021年3月20日15:45:35 发表评论 744 次浏览

date.now()

方法用于返回自1970年1月1日UTC 00:00:00以来经过的毫秒数。由于now()是Date的静态方法, 因此它将始终用作Date.now()。

语法如下:

var A = Date.now();

参数:

此方法不接受任何参数。

返回值:

它返回自1970年1月1日UTC 00:00:00以来经过的毫秒数。

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

程序1:

要获取当前日期, 请使用以下代码。

<script>
   // Use of Date.now() method
   var d = Date(Date.now());
  
   // Converting the number of millisecond 
   // in date string
   a = d.toString()
  
   // Printing the current date                    
   document.write( "The current date is: " + a) 
  
</script>

输出如下:

The current date is: Fri Jun 22 2018 10:54:33 
GMT+0530 (India Standard Time)

程式2:Date(Date.now())与Date()相同, 因此可以使用以下代码获得相同的结果, 即当前日期。

<script>
   // Using Date() method
   var d = Date();
    
   // Converting the number value to string
   a = d.toString() 
    
   // Printing the current date
   document.write( "The current date is: " + a)
    
</script>

输出如下:

The current date is: Fri Jun 22 2018 11:02:01 
GMT+0530 (India Standard Time)

支持的浏览器:支持的浏览器JavaScript Date now()方法下面列出:

  • 谷歌浏览器
  • Internet Explorer 9.0
  • 火狐浏览器
  • 歌剧
  • 苹果浏览器

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: