js知识点 Ⅶ

109 阅读1分钟

日期对象

  • 如何实例化日期对象?
    • Date() 实例化日期对象
    • 参数:任意时间的日期字符
    • 例:var date = new Date('2020-11-10 12:00:00')
  • 如何获取期当前时间?
    • var nowDate = new Date();
  • 日期对象下有哪些属性?
    • getFullYear() 返回年份,4位数,建议使用
    • getMonths() 返回月份,其值范围为0~11
    • getDate() 返回日期对象中的一个月中的第几天
    • getDay() 返回星期中的某一天,0~6
    • getHours() 返回日期对象中的小时部分
    • getMinutes() 返回日期对象中的分钟部分
    • getSeconds() 返回日期对象中的秒钟部分
    • getMillseconds() 返回日期对象中的毫秒部分
    • getTime() 返回日期对象中的时间戳的毫秒数
    • getTimezoneOffset() 返回日期对象中的时区的时差数,单位是分
  • 如何获取年,月,日,时,分,秒?
    • var year = nowDate.getFullYear(); //注意 月份返回值 从0-11 需要+1
    • var month = nowDate.getMonth()+1;//0-11
    • var da = nowDate.getDate();
    • var hours = nowDate.getHours();
    • var minutes = nowDate.getMinutes();
    • var seconds = nowDate.getSeconds();
    • console.log(year+'-'+month+'-'+da+' '+hours+':'+minutes+':'+seconds)