This article brings you a detailed explanation of the Date object in JavaScript (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Create a Date instance object
1.new Date(); Create a Date object based on the current time set by the system
let today = new Date(); console.log(today); //Thu Jun 14 2018 14:51:00 GMT+0800 (CST)
2.new Date(value);
Parameter: value represents since January 1, 1970 00:00:00 (Universal Standard Time) The number of milliseconds passed
let day = new Date(1528959690000); console.log(day); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
3.new Date(dateString);
Parameter: dateString A string value representing the date. This string should be recognized by the Date.parse() method
let day1 = new Date("2018-06-14T07:01:30.000Z"); console.log(day1); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
4.new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]] ]);
Parameters:
year represents the integer value of the year. In order to avoid the 2000 problem, it is best to specify a 4-digit year; use 1998, Instead of using 98
month represents the integer value of the month from 0 (January) to 11 (December)
day represents a month The integer value of the day in , starting from 1
hour The integer value representing the number of hours in a day (24-hour format)
minute Number of minutes
second Number of seconds
millisecond An integer value representing the millisecond part of time
let day2 = new Date(2018,5,14); console.log(day2); //Thu Jun 14 2018 00:00:00 GMT+0800 (CST) let day3 = new Date(2018,5,14,15,1,30); console.log(day3); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
2. Date static properties
##1.Date.prototype represents the prototype of the Date constructor
Date Instances inherit from Date.prototype. You can use the prototype object of the constructor to add properties or methods to all Date instances3. Date static methods
1.Date.UTC() method returns the number of milliseconds from 1970-1-1 00:00:00 UTC to the specified date
Syntax: Date.UTC(year, month[,date[,hrs[,min[,sec[,ms]]]]])Parameters:
- year A certain year after 1900
- month An integer between 0 and 11, indicating the month
- date An integer between 1 and 31, indicating the day of the month Day
- hrs An integer between 0 and 23, representing hours
- min An integer between 0 and 59, representing minutes
- sec An integer between 0 and 59, representing seconds
- ms An integer between 0 and 999, representing milliseconds
let utcDate = new Date(Date.UTC(2018, 5, 14, 7, 1, 30)); console.log(Date.UTC(2018, 5, 14, 7, 1, 30)); //1528959690000 console.log(utcDate); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST)
2.Date.now() method returns the number of milliseconds from 00:00:00 UTC on January 1, 1970 to the current time
let nowDate = new Date(Date.now()); console.log(nowDate); //Fri Jun 15 2018 16:31:05 GMT+0800 (CST) console.log(Date.now()); //1529051465358
3.Date.parse() method returns a value representing the number of milliseconds from 1970-1-1 00:00:00 UTC to the time represented by the given date string
Syntax: Date.parse(dateString)Parameters: dateString A string that conforms to RFC2822 or ISO 8601 date formatReturn value: Returns a string from 1970-1-1 00:00:00 The number of milliseconds from UTC to the time represented by the given date string. If the parameter cannot be parsed into a valid date, NaNlet time = Date.parse('2018-06-14T07:01:30.000Z'); console.log(time);// 1528959690000
is returned. 4. Date instance method
1.Year
setFullYear() method sets the year for a date object based on local time
Syntax: dateObj.setFullYear(yearValue[, monthValue[, dayValue]])Parameters:
- yearValue Specifies the integer value of the year, such as 1995.
- monthValue An integer value between 0 and 11, representing January to December.
- dayValue An integer value between 1 and 31, indicating the day of the month. If you specify the dayValue parameter, you must also specify the monthValue
The getFullYear() method returns the year of the specified date based on local time
Syntax: dateObj.getFullYear()Return value: Returns a year number corresponding to the given date based on local time
let date1 = new Date(1528959690000); console.log(date1); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date1.setFullYear(2017)); //1497423690000 console.log(date1.getFullYear()); //2017
setUTCFullYear() method sets the year for a specific date based on universal standard time
Syntax: dateObj.setUTCFulYear(yearValue[, monthValue[, dayValue]])Parameters:
- yearValue Specifies the year integer value, for example, 1995
- monthValue Optional, specify an integer value between 0-11, representing January to December
- dayValue Optional , specify an integer value between 1-31, representing the day of the month. If you specify the dayValue parameter, then you must specify the monthValue parameter
getUTCFulYear( ) Based on universal time, returns the year of a specified date object
Syntax: dateObj.getUTCFullYear()Return value: Returns an absolute value, conforming to the Year-2000 standard, such as 1995
let day1 = new Date(1528959690000); console.log(day1.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT console.log(day1.setUTCFullYear(2017)); //1497423690000 console.log(day1.getUTCFullYear()); //2017
2. Month
setMonth() method sets the month for a date object with a set year based on local time
Syntax: dateObj.setMonth(monthValue[, dayValue])Parameters:
monthValue 介于 0 到 11 之间的整数(表示一月到十二月)
dayValue 从 1 到 31 之间的整数,表示月份中的第几天
getMonth() 方法根据本地时间,返回一个指定的日期对象的月份
语法:dateObj.getMonth()
返回值:返回一个0 到 11的整数值: 0 代表一月份,1 代表二月份, 2 代表三月份,依次类推
let date2 = new Date(1528959690000); console.log(date2); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date2.setMonth(4)); //1526281290000 console.log(date2.getMonth()); //4
setUTCMonth()方法根据通用的时间来设置一个准确的月份
语法:dateObj.setUTCMonth(monthValue[, dayValue])
参数:
monthValue 一个0-11的整数,代表1月到12月
dayValue 可选参数,一个1-31的整数,代表一个月的天数
getUTCMonth方法以世界时为标准,返回一个指定的日期对象的月份,它是从 0 开始计数的(0 代表一年的第一个月)
语法:dateObj.getUTCMonth()
返回值:返回一个 0 到 11 的整数,分别对应以下月份:0 代表一月,1 代表二月,2 代表三月,依次类推
let day2 = new Date(1528959690000); console.log(day2.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT console.log(day2.setUTCMonth(4)); //1526281290000 console.log(day2.getUTCMonth()); //4
3.日
setDate() 方法根据本地时间来指定一个日期对象的天数
语法:dateObj.setDate(dayValue)
参数:dayValue 一个整数,表示该月的第几天
getDate() 根据本地时间,返回一个指定的日期对象为一个月中的第几天
语法:dateObj.getDate()
返回值:返回一个1 到 31的整数值
let date3 = new Date(1528959690000); console.log(date3); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date3.setDate(21)); //1529564490000 console.log(date3.getDate()); //21
setUTCDate() 方法就是根据全球时间设置特定date对象的日期
语法:dateObj.setUTCDate(dayValue)
参数:dayValue 一个1-31的整形数字,用来指定日期
getUTCDate() 方法以世界时为标准,返回一个指定的日期对象为一个月中的第几天
语法:dateObj.getUTCDate()
返回值:返回一个 1 到 31 的整数值
let day3 = new Date(1528959690000); console.log(day3.toUTCString()); ////Thu, 14 Jun 2018 07:01:30 GMT console.log(day3.setUTCDate(25)); //1529910090000 console.log(day3.getUTCDate()); //25
4.星期几
getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天
语法:dateObj.getDay()
返回值:返回一个整数值: 0 代表星期日, 1 代表星期一,2 代表星期二, 依次类推
let date = new Date(1528959690000); console.log(date); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date.getDay()); //4
getUTCDay() 方法以世界时为标准,返回一个指定的日期对象为一星期中的第几天,其中 0 代表星期天
语法:dateObj.getUTCDay()
返回值:返回一个对应一星期中第几天的整数:0 代表星期天,1 代表星期一,2 代表星期二,依次类推
5.时
setHours() 方法根据本地时间为一个日期对象设置小时数
语法:dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
参数:
hoursValue 一个 0 到 23 的整数,表示小时
minutesValue 一个 0 到 59 的整数,表示分钟
secondsValue 一个 0 到 59 的整数,表示秒数,如果指定了 secondsValue 参数,则必须同时指定 minutesValue 参数
msValue 一个 0 到 999 的数字,表示微秒数,如果指定了 msValue 参数,则必须同时指定 minutesValue 和 secondsValue 参数
getHours() 方法根据本地时间,返回一个指定的日期对象的小时
语法:dateObj.getHours()
返回值:返回一个0 到 23之间的整数值
let date4 = new Date(1528959690000); console.log(date4); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date4.setHours(11)); //1528945290000 console.log(date4.getHours()); //11
setUTCHours() 方法就是根据全球时间设置特定date对象的小时数
语法:dateObj.setUTCHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
参数:
hoursValue 表示小时的整数,取值0到23之间
minutesValue 可选参数,表示分钟的整数,取值0到59之间
secondsValue 可选参数,表示秒数的整数,取值0到59之间;如果指定了该参数,就要同时指定minutesValue这个参数
msValue 可选参数,表示毫秒的整数,取值0到999之间;如果指定了该参数,就要指定minutesValue和secondsValue这两个参数
getUTCHours() 方法以世界时为标准,返回一个指定的日期对象的小时数
语法:dateObj.getUTCHours()
返回值:返回一个 0 到 23 的整数
let day4 = new Date(1528959690000); console.log(day4.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT console.log(day4.setUTCHours(15)); //1528988490000 console.log(day4.getUTCHours()); //15
6.分
setMinutes() 方法根据本地时间为一个日期对象设置分钟数
语法:dateObj.setMinutes(minutesValue[, secondsValue[, msValue]])
参数:
minutesValue 一个 0 到 59 的整数,表示分钟数。
secondsValue 一个 0 到 59 的整数,表示秒数,如果指定了 secondsValue 参数,则必须同时指定 minutesValue 参数。
msValue 一个 0 到 999 的数字,表示微秒数,如果指定了 msValue 参数,则必须同时指定 minutesValue和secondsValue 参数
getMinutes() 方法根据本地时间,返回一个指定的日期对象的分钟数
语法:dateObj.getMinutes()
返回值:返回一个0 到 59的整数值
let date5 = new Date(1528959690000); console.log(date5); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date5.setMinutes(30)); //1528961430000 console.log(date5.getMinutes()); //30
setUTCMinutes()方法会根据根据全球时间来设置指定日期的分钟数
语法:dateObj.setUTCMinutes(minutesValue[, secondsValue[, msValue]])
参数:
minutesValue 必填,表示要设置的分钟数,是一个介于0和59之间的整数
secondsValue 可选参数,表示要设置的秒数,同样也是一个介于0和59之间的整数,如果你传入了这个参数,那么你就必须要传入minutesValue
msValue 可选参数,表示要设置的毫秒数,这是一个介于0和999之间的数字,如果你传入了这个参数,那么你就必须要传入minutesValue和secondsValue
getUTCMinutes() 方法以世界时为标准,返回一个指定的日期对象的分钟数
语法:dateObj.getUTCMinutes()
返回值:返回一个 0 到 59 的整数
let day5 = new Date(1528959690000); console.log(day5.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT console.log(day5.setUTCMinutes(45)); //1528962330000 console.log(day5.getUTCMinutes()); //45
7.秒
setSeconds() 方法根据本地时间设置一个日期对象的秒数
语法:dateObj.setSeconds(secondsValue[, msValue])
参数:
secondsValue 一个 0 到 59 的整数
msValue 一个 0 到 999 的数字,表示微秒数
getSeconds() 方法根据本地时间,返回一个指定的日期对象的秒数
语法:dateObj.getSeconds()
返回值:返回一个 0 到 59 的整数值
let date6 = new Date(1528959690000); console.log(date6); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date6.setSeconds(40)); //1528959700000 console.log(date6.getSeconds()); //40
setUTCSeconds() 方法为一个依据国际通用时间的特定日期设置秒数
语法:dateObj.setUTCSeconds(secondsValue[, msValue])
参数:
secondsValue 一个在0到59之间的整数,表示秒数
msValue 可选参数,一个0到999之间的数字,代表毫秒数
getUTCSeconds() 方法以世界时为标准,返回一个指定的日期对象的秒数
语法:dateObj.getUTCSeconds()
返回值:返回一个 0 到 59 的整数
let day6 = new Date(1528959690000); console.log(day6.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT console.log(day6.setUTCSeconds(50)); //1528959710000 console.log(day6.getUTCSeconds()); //50
8.毫秒
setMilliseconds() 方法会根据本地时间设置一个日期对象的豪秒数
语法:dateObj.setMilliseconds(millisecondsValue)
参数:millisecondsValue 一个 0 到 999 的数字,表示豪秒数
getMilliseconds() 方法,根据本地时间,返回一个指定的日期对象的毫秒数
语法:dateObj.getMilliseconds()
返回值:方法返回一个0 到 999的整数
let date7 = new Date(1528959690000); console.log(date7); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date7.setMilliseconds(100)); //1528959690100 console.log(date7.getMilliseconds()); //100
setUTCMilliseconds() 方法会根据世界时来设置指定时间的毫秒数
语法:dateObj.setUTCMilliseconds(millisecondsValue)
参数:millisecondsValue 0 - 999 之间的数值,代表毫秒数
getUTCMilliseconds() 方法以世界时为标准,返回一个指定的日期对象的毫秒数
语法:dateObj.getUTCMilliseconds()
返回值:返回一个 0 到 999 的整数
let day7 = new Date(1528959690000); console.log(day7.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT console.log(day7.setUTCMilliseconds(900)); //1528959690900 console.log(day7.getUTCMilliseconds()); //900
9.time
setTime() 方法以一个表示从1970-1-1 00:00:00 UTC计时的毫秒数为来为 Date 对象设置时间
语法:dateObj.setTime(timeValue)
参数:timeValue 一个整数,表示从1970-1-1 00:00:00 UTC开始计时的毫秒数
getTime() 方法返回一个时间的格林威治时间数值,这个方法的功能和 valueOf() 方法一样
语法:dateObj.getTime()
返回值:返回一个从1970年1月1日0时0分0秒(UTC,即协调世界时)距离该日期对象所代表时间的毫秒数
let date8 = new Date(1528959690000); console.log(date8); //Thu Jun 14 2018 15:01:30 GMT+0800 (CST) console.log(date8.setTime(1528959690100)); //1528959690100 console.log(date8.getTime()); //1528959690100
10.获取时间字符串
toDateString() 方法以美式英语和人类易读的形式返回一个日期对象日期部分的字符串
toTimeString() 方法以人类易读形式返回一个日期对象时间部分的字符串
toUTCString() 方法把一个日期转换为一个字符串,使用UTC时区
toISOString() 方法返回一个 ISO(ISO 8601 Extended Format)格式的字符串:
YYYY-MM-DDTHH:mm:ss.sssZtoJSON() 方法返回 Date 对象的字符串形式
toLocaleString() 方法返回该日期对象的字符串,该字符串格式因不同语言而不同
toLocaleDateString() 方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同
toLocaleTimeString() 方法返回该日期对象时间部分的字符串,该字符串格式因不同语言而不同
valueOf() 方法返回一个 Date 对象的原始值
let day9 = new Date(1528959690000); console.log(day9); //Thu Jun 14 2018 15:01:30 GMT+0800 (中国标准时间) console.log(day9.toString()); //Thu Jun 14 2018 15:01:30 GMT+0800 (中国标准时间) console.log(day9.toDateString()); //Thu Jun 14 2018 console.log(day9.toTimeString()); //15:01:30 GMT+0800 (中国标准时间) console.log(day9.toUTCString()); //Thu, 14 Jun 2018 07:01:30 GMT console.log(day9.toISOString()); //2018-06-14T07:01:30.000Z console.log(day9.toJSON()); //2018-06-14T07:01:30.000Z console.log(day9.toLocaleString()); //2018/6/14 下午3:01:30 console.log(day9.toLocaleDateString()); //2018/6/14 console.log(day9.toLocaleTimeString()); //下午3:01:30 console.log(day9.valueOf()); //1528959690000
The above is the detailed content of Detailed explanation of Date object in JavaScript (with examples). For more information, please follow other related articles on the PHP Chinese website!

如果您正在寻找根据系统时间戳自动创建文件和文件夹并为其命名的方法,那么您来对地方了。有一种超级简单的方法可以用来完成这项任务。然后,创建的文件夹或文件可用于各种目的,例如存储文件备份、根据日期对文件进行排序等。在本文中,我们将通过一些非常简单的步骤解释如何在Windows11/10中自动创建文件和文件夹,并根据系统的时间戳对其进行命名。使用的方法是批处理脚本,非常简单。希望你喜欢阅读这篇文章。第1节:如何根据系统当前时间戳自动创建文件夹并命名第1步:首先,导航到要在其中创建文件夹的父文件夹,

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

在使用PHP程序开发时,经常会碰到一些警告或者错误的提示信息。其中,可能出现的一个错误提示就是:PHPWarning:date()expectsparameter2tobelong,stringgiven。这个错误的提示信息意思是:函数date()的第二个参数期望是长整型(long),但是实际传递给它的是字符串(string)。那么,我们

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

一.介绍java.util包中的Date类表示特定的时间,精确到毫秒。如果要想使用我们的Date类,那么我们必须得引入我们的Date类。Date类直接写入年份是得不到正确的结果的。因为java中Date是从1900年开始算的,所以前面的第一个参数只要填入从1900年后过了多少年就是你想要得到的年份。月需要减1,日可以直接插入。这种方法用的比较少,常用的是第二种方法。这种方法是将一个符合特定格式,比如yyyy-MM-dd,的字符串转化成为Date类型的数据。首先,定义一个Date类型的对象Date

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

Python中有许多优秀的日历库和日期库供我们使用,这些库可以帮助我们处理日期和日历相关的操作。接下来,我将为大家介绍几个常用的选择,并提供相应的代码示例。datetime库:datetime是Python内置的日期和时间处理模块,提供了许多日期和时间相关的类和方法,可以用于处理日期、时间、时间差等操作。示例代码:importdatetime#获取当


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools
