首页 > 技术文章 > JavaScript——Date日期对象

clblogs 2021-05-11 20:29 原文

  标准对象

typeof 123
"number"
typeof '123' "string"
typeof true "boolean"
typeof NaN "number"
typeof [] "object"
typeof {} "object"
typeof Math.abs "function"
typeof undefined "undefined"

 

  基本使用

  var now=new Date();    //中国标准时间
  now.getFullYear();   //
  now.getMonth();    //月    0~11代表月份
  now.getDate();    //
  now.getDay();     //星期几
  now.getHours();   //
  now.getMinutes();  //
  now.getSeconds();  //

  now.getTime();    //时间戳   全世界统一   1970 1.1 0:00:00 毫秒数

  console.log(new Date(1620735319811))   //时间戳转换为时间

 

   转换

now=new Date(1620735319811)
Tue May 11 2021 20:15:19 GMT+0800 (中国标准时间)

now.toLocaleDateString      //注意:调用是一个方式,不是一个属性
ƒ toLocaleDateString() { [native code] }

now.toLocaleDateString()
"2021/5/11"

now.toGMTString()
"Tue, 11 May 2021 12:15:19 GMT"

 

推荐阅读