Js new Date()

转载 · 默小柒 · 2022-09-05 · 原文

1.参数形式有以下5种:

new Date("month dd,yyyy hh:mm:ss");

new Date("month dd,yyyy");

new Date(yyyy,mth,dd,hh,mm,ss);

new Date(yyyy,mth,dd);

new Date(ms);  (参数表示需要创建的时间和GMT时间1970年1月1日之间相差的毫秒数。)

2.各种函数的含义如下:

month:用英文表示月份名称,从January到December

mth:用整数表示月份,从(1月)到11(12月)

dd:表示一个月中的第几天,从1到31

yyyy:四位数表示的年份

hh:小时数,从0(午夜)到23(晚11点)

mm:分钟数,从0到59的整数

ss:秒数,从0到59的整数

ms:毫秒数,为大于等于0的整数



Date() 返回当日的日期和时间。


getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。

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

getMonth() 从 Date 对象返回月份 (0 ~ 11)。

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

getYear() 请使用 getFullYear() 方法代替。

getHours() 返回 Date 对象的小时 (0 ~ 23)。

getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。

getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。

getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。

getTime() 返回 1970 年 1 月 1 日至今的毫秒数。

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

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

getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。

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

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

getUTCHours() 根据世界时返回 Date 对象的小时 (0 ~ 23)。

getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。

getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。

getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。

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

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

setMonth() 设置 Date 对象中月份 (0 ~ 11)。

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

setYear() 请使用 setFullYear() 方法代替。

setHours() 设置 Date 对象中的小时 (0 ~ 23)。

setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。

setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。

setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。

setTime() 以毫秒设置 Date 对象。

setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。

setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。

setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。

setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)。

setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。

setUTCSeconds() 根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。

setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。

toSource() 返回该对象的源代码。

toString() 把 Date 对象转换为字符串。

toTimeString() 把 Date 对象的时间部分转换为字符串。

toDateString() 把 Date 对象的日期部分转换为字符串。

toGMTString() 请使用 toUTCString() 方法代替。 1 3

toUTCString() 根据世界时,把 Date 对象转换为字符串。

toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。

toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。

toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。

UTC() 根据世界时返回 1997 年 1 月 1 日 到指定日期的毫秒数。

valueOf() 返回 Date 对象的原始值。

var objDate=new Date([arguments list]);

new Date()参数格式如下:

1、用整数初始化日期对象

var date1 = new Date(2017,06,06); console.log(date1); // Thu Jul 06 2017 00:00:00 GMT+0800 (中国标准时间) 

var date1 = new Date(2017,1,1); console.log(date1); // Wed Feb 01 2017 00:00:00 GMT+0800 (中国标准时间) 

var date1 = new Date(2017,01-2,01); console.log(date1); // Thu Dec 01 2016 00:00:00 GMT+0800 (中国标准时间) 

var date1 =new Date(2017,06,06,06,06,06); console.log(date1); // Thu Jul 06 2017 06:06:06 GMT+0800 (中国标准时间)

说明: new Date( year, month, date, hrs, min, sec) 按给定的参数创建一日期对象

2、用字符串初始化日期对象

var date2 = new Date(“2017/06/06”); console.log(date2); // Tue Jun 06 2017 00:00:00 GMT+0800 (中国标准时间) 

var date2 = new Date(“2017-08-08”); console.log(date2); // Tue Aug 08 2017 08:00:00 GMT+0800 (中国标准时间) 

var date2 = new Date(“2017-9-9”); console.log(date2); // Sat Sep 09 2017 00:00:00 GMT+0800 (中国标准时间)

说明:如果字符串模式不支持短横杠模式,则进行字符串替换:

var strTime=”2011-04-16”; 
var date2= new Date(Date.parse(strTime.replace(/-/g, “/”))); // /-/g为正则表达式(RegExp) 对象,表示全局替换-为/。

3、用毫秒时间戳初始化日期对象

var timestamp=new Date().getTime(); console.log( new Date(timestamp) ); //Tue Jun 06 2017 11:06:59 GMT+0800 (中国标准时间) 

var date3 = new Date( timestamp - 1 * 60 * 60 * 1000); console.log(date3); // Tue Jun 06 2017 10:06:59 GMT+0800 (中国标准时间)

说明:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。时间戳唯一地标识某一刻的时间。