Javascript Date 对象相关知识

Javascript Date 对象相关知识

参考文章@虹猫1992

创建 Date 对象.

方法一:

自动使用当前的日期和时间作为其初始值.

var date = new Date();

方法二:将给定的毫秒数转换为使用的时间,new Date(dateVal)

  • 如果是数字值,dateVal表示指定日期与1970年1月1日午夜间全球标准时间的毫秒数
  • 如果是字符串,则 dateVal 按照 parse 方法中的规则进行解析。
var date = new Date("2018/04/06 03:23:55");
var date = new Date(1545548361287);

方法三:

指定具体的日期,new Date((year,month,date[,hours[,minutes[,seconds[,ms]]]]))

year 必选项,完整的年份,比如,1976(而不是 76

month 必选项,表示的月份,是011 之间的整数1 月至 12 月)

date 必选项,表示日期,是从 131 之间的整数

hours 可选项,如果提供了 minutes 则必须给出。表示小时,是从 023 的整数

minutes 可选项,如果提供了 seconds 则必须给出。表示分钟,是从 059 的整数

seconds 可选项,如果提供了 milliseconds 则必须给出。表示秒钟,是从 059 的整数

ms 可选项,表示毫秒,是从 0999 的整数

var date = new Date(2018, 11, 23, 15, 3, 23);

日期运算

两个日期示例对象进行运算

  • 加法:返回的是两个字符串连接而成的新字符串。
  • 减法:返回的是它们间隔的毫秒数;
var d1 = new Date(2018, 0, 1);
var d2 = new Date(2018, 11, 1);

console.log(d2 - d1); //28857600000
console.log(d2 + d1); 
// "Sat Dec 01 2018 00:00:00 GMT+0800 (中国标准时间)Mon Jan 01 2018 00:00:00 GMT+0800 (中国标准时间)"

静态方法

Date.now() : 返回当前时间距离时间零点(19701100:00:00 UTC)的毫秒数,相当于 Unix 时间戳乘以1000

Date.now(); // 1545549988630

继承方法

valueOf(): 返回实例对象距离时间零点(19701100:00:00 UTC)对应的毫秒数,该方法等同于getTime方法。

var d = new Date();

d.valueOf() // 1545550458280
d.getTime() // 1545550458280

toString()方法返回一个完整的日期字符串。

var d = new Date(2018, 11, 23);

console.log(d.toString()); // Sun Dec 23 2018 00:00:00 GMT+0800 (中国标准时间)
console.log(d); // Sun Dec 23 2018 00:00:00 GMT+0800 (中国标准时间)

toLocaleString()返回一个表示该日期对象的字符串,该字符串与系统设置的地区关联。

var d = new Date(2018, 11, 23);

console.log(d.toLocaleString()); // 2018/12/23 上午12:00:00

格式化方法

参考文章@lsxj

let d = new Date();
console.log(d.toLocaleString()); // 2023/9/19 上午11:26:21
console.log(d.toString()); // Tue Sep 19 2023 11:26:53 GMT+0800 (中国标准时间)
console.log(d.toDateString()); // Tue Sep 19 2023
console.log(d.toTimeString()); // 11:27:27 GMT+0800 (中国标准时间)
console.log(d.toLocaleDateString()); // 2023/9/19
console.log(d.toLocaleTimeString()); // 上午11:27:56
console.log(d.toUTCString()); // Tue, 19 Sep 2023 03:28:11 GMT

get 类方法

Date对象提供了一系列get*方法,用来获取实例对象某个方面的值

实例的方法名 作用
getTime() 返回实例距离1970年1月1日00:00:00的毫秒数,等同于valueOf方法
getYear() 返回距离1900的年数
getFullYear() 返回四位的年份
getMonth() 返回月份(0 ~ 11,0表示1月,11表示12月)
getDay() 返回星期几,星期日为0,星期一为1,以此类推
getDate() 返回实例对象对应每个月的几号(从1开始)
getHours() 返回小时数 (0 ~ 23)
getMinutes() 返回分钟数 (0 ~ 59)
getSeconds() 返回秒数 (0 ~ 59)
getMilliseconds() 返回毫秒数 (0 ~ 999)

set 类方法

Date对象提供了一系列set*方法,用来设置实例对象的各个方面。

实例的方法名 作用
setTime(milliseconds) 设置毫秒时间戳
setYear(year) 设置距离1900年的年数
setFullYear(year [, month, date]) 设置四位年份
setMonth(month [, date]) 设置月份(0-11)
setDate(date) 设置实例对象对应的每个月的几号(1-31),返回改变后毫秒时间戳
setHours(hour [, min, sec, ms]) 设置小时(0-23)
setMinutes(min [, sec, ms]) 设置分钟(0-59)
setSeconds(sec [, ms]) 设置秒(0-59)
setMilliseconds() 设置毫秒(0-999)

date 相关工具方法

date 获取当天零点时间

参考文章@yujin0213


//获取当天零点的时间
const stamp1 = new Date(new Date().setHours(0, 0, 0, 0));
//获取当天23:59:59的时间
const stamp2 = new Date(
  new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1
);

猜你喜欢

转载自blog.csdn.net/i_Satan/article/details/133020490