Skip to content

Date常用方法

INFO

首先需要定义一个变量:

js
const date = new Date();

INFO

  • Date(): 返回当日的日期和时间;

  • getDate(): 从 Date 对象返回一个月中的某一天(1~31)console.log(date.getDate())

  • getDay(): 从 Date 对象返回一周中的某一天(0~6);

  • getMonth(): 从 Date 对象返回月份(0~11);

  • getFullYear(): 从 Date 对象以四位数字返回年份;

  • getYear():可以使用 getFullYear() 代替;

  • getHours(): 返回 Date() 对象的小时(0~23);

  • getMinutes(): 返回 Date() 对象的分钟(0~59);

  • getSeconds(): 返回 Date() 对象的分钟(0~59);

  • getMillseconds(): 返回 Date() 对象的毫秒(0~999);

  • getTime(): 返回 1970 年 1 月 1 日至今的时间;

  • getTimezoneOffset(): 返回本地时间与格林威治标准时间(GMT)的分钟差;

  • getUTCDate(): 根据世界时从 Date 对象返回月中的一天(1~31);

  • getUTCDay(): 根据世界时从 Date 对象返回周中的一天(1~6);

  • getUTCMonth(): 根据世界时从 Date 对象返回月份(0~11);

  • getUTCFullYear(): 根据世界时从 Date 对象返回四位数的年份;

  • getUTCHours(): 根据世界时从 Date 对象返回对象的小时(0~23);

  • getUTCMinutes(): 根据世界时从 Date 对象返回对象的分钟(0~59);

  • getUTCSeconds(): 根据世界时从 Date 对象返回对象的秒钟(0~59);

  • getUTCMillseconds(): 根据世界时从 Date 对象返回对象的毫秒(0~999);

  • parse(): 返回 1970 年 1 月 1 日午夜到指定日期(字符串)的毫秒数;

  • setDate(): 设置 Date 对象中月的某一天(1~31);

  • setMonth(): 设置 Date 对象中月份(0~11);

  • setFullYear(): 设置 Date 对象中的年份(四位数字);