Js中Date对象的使用

目录

        1.什么是Date对象

        2.如何声明Date对象

        3.Date常用方法

                getDate()

                getDay()

                getFullYear()

                getHours()

                getMilliseconds()

                getMinutes()

                getMonth()

扫描二维码关注公众号,回复: 14970153 查看本文章

                getSeconds()

                getTime()


        1.什么是Date对象

                        创建一个 JavaScript Date 实例,该实例呈现时间中的某个时刻。Date 对象则基于 Unix Time Stamp,即自1970年1月1日零时起(UTC)起经过的毫秒数。

        2.如何声明Date对象

                        一般使用下面的四种方法,第一种是获取程序当时运行时的时间点,后面三种方式都是给了参数后,获取到对应参数的值。

var date = new Date();
var date = new Date(value);
var date = new Date(dateString);
var date = new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);



var today = new Date()    //程序运行时的时间点
var d1 = new Date("October 13, 1975 11:13:00")   //1975-10-13 11:13:00
var d2 = new Date(79,5,24)    //1979-06-24
var d3 = new Date(79,5,24,11,33,0) //1979-06-24 11:33:00   []表示可选填
//这里说一点,月份的设置符合要求的值不是1-12,而是0-11。一年中的第一个月是0,最后一个月是11
//月份的区别就表现在以数字形式输入,就需要注意月份是-1。当以字符串形式输入时,输入几月就是几月

        3.Date常用方法

                getDate()

                        根据本地时间或者设置的时间,获取指定对象中的哪一日,并以数值(1-31)返回参数

const birthday = new Date('August 19, 1975 23:15:30');
const d1 = birthday.getDate();

console.log(date1);
// 输出的是 19

                getDay()

                        根据本地时间或者设置的时间,获取指定对象是在当时时间属于周几,并以数值(0-6)返回,周一返回1,周日返回0     

const birthday = new Date('August 19, 1975 23:15:30');
const day1 = birthday.getDay();
// Sunday - Saturday : 0 - 6

console.log(day1);
//输出 2  星期二

                getFullYear()

                        根据本地时间或者设置的时间,返回指定对象的年份         

const moonLanding = new Date('July 20, 69 00:20:18');
const y = moonLanding.getFullYear()

console.log(y);
// 输出: 1969

                getHours()

                     根据本地时间或者设置的时间,返回指定对象的小时

const birthday = new Date('March 13, 08 04:20');
const h = birthday.getHours()

console.log(h);
// 输出: 4

                getMilliseconds()

                        根据本地时间或者设置的时间,返回指定日期对象的毫秒值

const moonLanding = new Date('July 20, 69 00:20:18');
const ms = moonLanding.setMilliseconds(123);

console.log(ms);
// 输出: 123

                getMinutes()

                        根据本地时间或者设置的时间,返回指定日期对象的分钟数。

const birthday = new Date('March 13, 08 04:20');
const m = birthday.getMinutes()

console.log(m);
// 输出: 20

                getMonth()

                        根据本地时间或设置的时间,并以数值(0-11)返回指定日期对象的月份,0表示一年的第一个月,11表示一年中的最后一个月。

const moonLanding = new Date('December 20, 69 00:20:18');
const month = moonLanding.getMonth()

console.log(month); // (January 是 0)
// 输出: 11

                getSeconds()

                        根据本地时间或设置的时间,返回指定日期对象的秒数。

const moonLanding = new Date('July 20, 69 00:20:18');
const seconds = moonLanding.getSeconds()

console.log(seconds);
// 输出: 18

                getTime()

                        根据本地时间或设置的时间,返回一个格林威治时间数值,也就是自从 1970 年 1 月 1 日到指定时间对象的一个毫秒值。

const moonLanding = new Date('July 20, 69 20:17:40 GMT+00:00');
const hms = moonLanding.getTime()

// 毫秒值自 Jan 1, 1970, 00:00:00.000 GMT 开始计算  该值可以为负值,也就是指定日期在1970年1月1日前
console.log(hms);
// 输出: -14182940000

                需要注意的是获取星期几时,返回的是0-6的值,但是0代表的是周日,其他按照顺序排开;但是获取月份时,返回的是0-11的值,但是0代表一年中的第一个月,11代表最后一个月,这一点有点像数组的索引。

猜你喜欢

转载自blog.csdn.net/Jsy_997/article/details/124370679