Date function
new Date()
Date object will automatically save the current date and time as its initial value.
date.getDate()
Returns the day of the month (1 ~ 31) from the Date object.
date.getDay()
Returns the day of the week (0 ~ 6) from the Date object. Sunday is 0.
date.getMonth()
Returns the month (0 ~ 11) from the Date object.
date.getFullYear()
Return the year as a four-digit number from the Date object
date.getHours()
Return the hour of the Date object (0 ~ 23).
date.getMinutes()
Returns the minutes of the Date object (0 ~ 59).
date.getSeconds()
Returns the number of seconds in the Date object (0 ~ 59).
date.getMilliseconds()
Returns the milliseconds (0 ~ 999) of the Date object.
date.getTime()
Returns the number of milliseconds since January 1, 1970.
date.getTimezoneOffset()
Returns the time difference between Greenwich Mean Time (GMT or UTC) and local time, in minutes. The reason the return is in minutes, not hours, is that some
countries have time zones that are even less than an hour apart.
date.getUTCDate()
Returns the day of the month (1 ~ 31) from the Date object according to universal time.
date.getUTCDay()
Returns the day of the week (0 ~ 6) from the Date object based on universal time. Sunday is 0.
date.getUTCMonth()
Returns the month (0 ~ 11) from the Date object according to universal time.
date.getUTCFulYear()
Returns the four-digit year from the Date object according to universal time.
date.getUTCHours()
Returns the hour (0 ~ 23) of the Date object according to universal time.
date.getUTCMinutes()
Returns the minutes (0 ~ 59) of the Date object according to universal time.
date.getUTCSeconds()
Returns the seconds (0 ~ 59) of the Date object according to universal time.
date.getUTCMilliseconds()
Returns the milliseconds (0 ~ 999) of the Date object according to universal time.
Date.parse(string)
Returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string).
date.setDate(int)
Set a certain day of the month (1 ~ 31) in the Date object. Returns the adjusted millisecond representation of the date.
date.setMonth(month[, day])
Set the month (0 ~ 11) in the Date object. The day parameter is optional, indicating that a day of the month is between 1 and 31.
date.setFullYear(year[, month, day])
Set the year (four digits) in the Date object. The month parameter is optional, indicating that the month is between 0 and 11. The day parameter is optional, indicating that a day of the month is between 1 and 31.
date.setHours(hours[, minutes, seconds, milliseconds])
Set the hours (0 ~ 23) in the Date object. The minutes parameter is optional, indicating that the number of minutes is between 0 and 59. The seconds parameter is optional, indicating that the number of minutes is between 0
~ 59. The milliseconds parameter is optional and represents the number of minutes between 0 and 999.
date.setMinutes(minutes[, seconds, milliseconds])
Set the minutes (0 ~ 59) in the Date object. The seconds parameter is optional, indicating that the number of minutes is between 0 and 59. The milliseconds parameter is optional and represents the number of minutes between 0 and 999.
date.setSeconds(seconds[, milliseconds])
Set the seconds (0 ~ 59) in the Date object. The milliseconds parameter is optional and represents the number of minutes between 0 and 999.
date.setMilliseconds(int)
Set the milliseconds (0 ~ 999) in the Date object.
date.setTime(long)
The set date and time are the number of milliseconds between midnight and midnight on January 1, 1970, GMT time. Representing the date in milliseconds makes it time zone independent.
date.setUTCDate(int)
Set the day of the month (1 ~ 31) in the Date object according to universal time.
date.setUTCMonth(minutes[, seconds, milliseconds])
Set the month (0 ~ 11) in the Date object according to universal time. The seconds parameter is optional, indicating that the number of minutes is between 0 and 59. The milliseconds parameter is optional, and the table
indicates that the number of minutes is between 0 ~ 999.
date.setUTCFulYear(year[, month, day])
Set the year (four digits) in the Date object according to universal time. The month parameter is optional, indicating that the month is between 0 and 11. The day parameter is optional, indicating that the
day of the month is between 1 and 31.
date.setUTCHours(hours[, minutes, seconds, milliseconds])
Set the hours (0 ~ 23) in the Date object according to universal time. The minutes parameter is optional, indicating that the number of minutes is between 0 and 59. The seconds parameter is optional, indicating that the number of minutes is between 0 and 59. The milliseconds parameter is optional and represents the number of minutes between 0 and 999.
date.setUTCMinutes(minutes[, seconds, milliseconds])
根据世界时设置 Date 对象中的分钟 (0 ~ 59)。seconds参数可选,表示分钟数介于 0 ~ 59 之间。milliseconds参数可选,表
示分钟数介于 0 ~ 999 之间。
date.setUTCSeconds(seconds[, milliseconds])
根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。milliseconds参数可选,表示分钟数介于 0 ~ 999 之间。
date.setUTCMilliseconds(int)
根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
date.toString()
把 Date 对象转换为字符串。
date.toTimeString()
把 Date 对象的时间部分转换为字符串。
date.toDateString()
把 Date 对象的日期部分转换为字符串。
date.toUTCString()
根据世界时,把 Date 对象转换为字符串。
date.toLocaleString()
根据本地时间格式,把 Date 对象转换为字符串。
date.toLocaleTimeString()
根据本地时间格式,把 Date 对象的时间部分转换为字符串。
date.toLocaleDateString()
根据本地时间格式,把 Date 对象的日期部分转换为字符串。
Date.UTC(year,month,day[,hour,minute,second,milliseconds])
根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。年月日必填,时分秒毫秒都是选填的。
date.valueOf()
返回 Date 对象的原始值。与 Date.getTime 返回的值相等。即 1970 年 1 月 1 日至今的毫秒数。
<html> <head> <style type="text/css"> body { font-size:14px; } </style> </head> <body> <script type="text/javascript"> function write(name, value) { document.write(name + " = " + value + "<br/>"); } var date = new Date();//Date 对象会自动把当前日期和时间保存为其初始值。 write("new Date()", date); write("date.getDate()", date.getDate());//从 Date 对象返回一个月中的某一天 (1 ~ 31)。 write("date.getDay()", date.getDay());//从 Date 对象返回一周中的某一天 (0 ~ 6)。周日是0。 write("date.getMonth()", date.getMonth());//从 Date 对象返回月份 (0 ~ 11)。 write("date.getFullYear()", date.getFullYear());//从 Date 对象以四位数字返回年份。 write("date.getHours()", date.getHours());//返回 Date 对象的小时 (0 ~ 23)。 write("date.getMinutes()", date.getMinutes());//返回 Date 对象的分钟 (0 ~ 59)。 write("date.getSeconds()", date.getSeconds());//返回 Date 对象的秒数 (0 ~ 59)。 write("date.getMilliseconds()", date.getMilliseconds());//返回 Date 对象的毫秒(0 ~ 999)。 write("date.getTime()", date.getTime());//返回 1970 年 1 月 1 日至今的毫秒数。 write("date.getTimezoneOffset()/60", date.getTimezoneOffset()/60);//返回格林威治时间(GMT 或 UTC)和本地时间之间的时差,以分钟为单位。返回之所以以分钟计,而不是以小时计,原因是某些国家所占有的时区甚至不到一个小时的间隔。 write("date.getUTCDate()", date.getUTCDate());//根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。 write("date.getUTCDay()", date.getUTCDay());//根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。周日是0。 write("date.getUTCMonth()", date.getUTCMonth());//根据世界时从 Date 对象返回月份 (0 ~ 11)。 write("date.getUTCFullYear()", date.getUTCFullYear());//根据世界时从 Date 对象返回四位数的年份。 write("date.getUTCHours()", date.getUTCHours());//根据世界时返回 Date 对象的小时 (0 ~ 23)。 write("date.getUTCMinutes()", date.getUTCMinutes());//根据世界时返回 Date 对象的分钟 (0 ~ 59)。 write("date.getUTCSeconds()", date.getUTCSeconds());//根据世界时返回 Date 对象的秒钟 (0 ~ 59)。 write("date.getUTCMilliseconds()", date.getUTCMilliseconds());//根据世界时返回 Date 对象的毫秒(0 ~ 999)。 write("Date.parse(string)", Date.parse(date.toString()));//返回1970年1月1日午夜到指定日期(字符串)的毫秒数。 write("date.setDate(int)", date.setDate(1));//设置 Date 对象中月的某一天 (1 ~ 31)。返回调整过的日期的毫秒表示。 write("date.setMonth(int)", date.setMonth(0));//设置 Date 对象中月份 (0 ~ 11)。 write("date.setFullYear(int)", date.setFullYear(2017));//设置 Date 对象中的年份(四位数字)。 write("date.setHours(int)", date.setHours(0));//设置 Date 对象中的小时 (0 ~ 23)。 write("date.setMinutes(int)", date.setMinutes(0));//设置 Date 对象中的分钟 (0 ~ 59)。 write("date.setSeconds(int)", date.setSeconds(0));//设置 Date 对象中的秒钟 (0 ~ 59)。 write("date.setMilliseconds(int)", date.setMilliseconds(0));//设置 Date 对象中的毫秒 (0 ~ 999)。 write("date.setTime(long)", date.setTime(Date.UTC(2017,0,1)));//设置的日期和时间据 GMT 时间 1970 年 1 月 1 日午夜之间的毫秒数。以毫秒形式表示日期可以使它独立于时区。 write("date.setUTCDate(int)", date.setUTCDate(1));//根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。 write("date.setUTCMonth(int)", date.setUTCMonth(0));//根据世界时设置 Date 对象中的月份 (0 ~ 11)。 write("date.setUTCFullYear(int)", date.setUTCFullYear(2017));//根据世界时设置 Date 对象中的年份(四位数字)。 write("date.setUTCHours(int)", date.setUTCHours(0));//根据世界时设置 Date 对象中的小时 (0 ~ 23)。 write("date.setUTCMinutes(int)", date.setUTCMinutes(0));//根据世界时设置 Date 对象中的分钟 (0 ~ 59)。 write("date.setUTCSeconds(int)", date.setUTCSeconds(0));//根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。 write("date.setUTCMilliseconds(long)", date.setUTCMilliseconds(0));//根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。 write("date.toString()", date.toString());//把 Date 对象转换为字符串。 write("date.toTimeString()", date.toTimeString());//把 Date 对象的时间部分转换为字符串。 write("date.toDateString()", date.toDateString());//把 Date 对象的日期部分转换为字符串。 write("date.toUTCString()", date.toUTCString());//根据世界时,把 Date 对象转换为字符串。 write("date.toLocaleString()", date.toLocaleString());//根据本地时间格式,把 Date 对象转换为字符串。 write("date.toLocaleTimeString()", date.toLocaleTimeString());//根据本地时间格式,把 Date 对象的时间部分转换为字符串。 write("date.toLocaleDateString()", date.toLocaleDateString());//根据本地时间格式,把 Date 对象的日期部分转换为字符串。 write("Date.UTC(year,month,day[,hour,minute,second,milliseconds])", Date.UTC(2017,0,1,0,0,0));//根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。年月日必填,时分秒毫秒都是选填的。 write("date.valueOf()", date.valueOf());//返回 Date 对象的原始值。与 Date.getTime 返回的值相等。即 1970 年 1 月 1 日至今的毫秒数。 </script> </body> </html>
格式化Date
由于JS本身并不提供格式化日期的函数。因此必须借助prototype属性来添加自定义的日期格式化方法。
<html> <head> <meta charset="UTF8" /> </head> <body> <script type="text/javascript"> /* 年(y)可以用 1-4 个占位符 月(M)、日(d)、时(H,24时)、时(h,12时)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符 毫秒(S)只能用 1 个占位符(是 1-3 位数字) AM或PM只能用 1 个占位符(是 2 位英文) 上午或下午(T)只能用 1 个占位符(是 2 位中文) 星期(E)可以用 1-3 个占位符 季度(q)只能用 1 个占位符(是 1 位数字) */ Date.prototype.format = function(fmt) { var map = { "M+" : this.getMonth() + 1, //月 "d+" : this.getDate(), //日 "H+" : this.getHours(), //24时 /* 上午12时只代表当天上午的12时,下午的12时代表当天下午的12时, 0时代表第二天的开始,即前面一天12时已过0时开始计算新一天的时间, 虽然说时间上跟前面那一天下午12时重合,但日期已经发生更改,所以不能说0时就是12时 */ "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12,//12时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "S" : this.getMilliseconds(), //毫秒 "t" : this.getHours() < 12 ? "AM" : "PM", "T" : this.getHours() < 12 ? "上午" : "下午", }; var week = { "0" : "日", "1" : "一", "2" : "二", "3" : "三", "4" : "四", "5" : "五", "6" : "六", } var quarter = { "0" : "一", "1" : "二", "2" : "三", "3" : "四", } if(/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } if(/(E+)/.test(fmt)) { var weekPreStr; switch(RegExp.$1.length) { case 1: weekPreStr = ""; break; case 2: weekPreStr = "周"; break; default: weekPreStr = "星期"; break; } fmt = fmt.replace(RegExp.$1, weekPreStr + week[this.getDay()]); } if(/(q)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, quarter[Math.floor(this.getMonth() / 3)]); } for(var key in map) { if(new RegExp("(" + key + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? map[key] : ("00" + map[key]).substr((map[key]+"").length)); } } return fmt; } function write(str) { document.write(str + "<br/>"); } var date = new Date(); write(date.toString()); write(date.toUTCString()); write(date.toLocaleString()); write(""); write(date.format("yy-M-d t h:m:s.S")); write(date.format("yyyy-MM-dd hh:mm:ss.S")); write(date.format("yyyy-MM-dd E HH:mm:ss")); write(date.format("yyyy-MM-dd EE hh:mm:ss")); write(date.format("yyyy-MM-dd EEE hh:mm:ss")); write(date.format("yyyy年MM月dd日 EEE hh:mm:ss 第q季度")); </script> </body> </html>
对Date设置超出范围的参数
即使设置了超出范围的参数也不会报错,只会计算“最大合法值”与“超出值”相加得到的date。
<html> <body> <script type="text/javascript"> var date = new Date(); date.setFullYear(2016,12,32, 0, 0, 0); document.write(date); </script> </body> </html>
比较Date
<html> <head> <meta charset="UTF8" /> </head> <body> <script type="text/javascript"> var date1 = new Date(); var date2 = new Date(); date1.setTime(Date.UTC(2017,0,1)); date2.setTime(Date.UTC(2016,11,31)); document.write("date1 = " + date1.toLocaleString() + "<br/>"); document.write("date2 = " + date2.toLocaleString() + "<br/>"); document.write("date1 - date2 = " + (date1 - date2) + "毫秒<br/>"); document.write("date1 - date2 = " + (date1 - date2)/1000/60/60 + "小时<br/>"); document.write("date1 > date2 = " + (date1 > date2) + "<br/>"); document.write("date1 < date2 = " + (date1 < date2) + "<br/>"); </script> </body> </html>
以上所述是小编给大家介绍的JavaScript Date 知识浅析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
更多JavaScript Date 知识浅析相关文章请关注PHP中文网!

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


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

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

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