js基础之Date对象

103 阅读1分钟

###初始化日期对象

    const date = new Date() //如果没有参数为当前日期对象

常用的初始化带参数方式有:

image.png

###格式化日期

    const date = new Date();
    date.getFullYear(); //年
    date.getMonth()+1; //月
    date.getDate();  //日
    date.getDay();  //周
    date.getHours()    //h
    date.getMinutes()  //m
    date.getSeconds()  //s
**toLocaleDateString()**

 const date = new Date();
 date.toLocaleDateString();//"2023/4/22"
 
**toTimeString()**

 date.toTimeString();//'14:59:02 GMT+0800 (中国标准时间)'

**toLocalString()**

 date.toLocaleString();// '2023/4/22 15:01:17'
 

###获取毫秒数

const date = new Date();
date.getTime()
date.valueOf()
    

算固定的某一天距离当前时间的天数

** 使用时间戳处以246060*1000**

   const start = new Date('2021-11-11')

   const end = new Date()

  const dates = Math.floor((end - start) / (1000 * 606024)) //天