Home Web Front-end JS Tutorial A brief analysis of JavaScript Date knowledge

A brief analysis of JavaScript Date knowledge

Feb 03, 2017 pm 01:52 PM

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>
Copy after login

格式化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>
Copy after login

对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>
Copy after login

A brief analysis of JavaScript Date knowledge

比较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>
Copy after login

JavaScript Date 知识浅析

以上所述是小编给大家介绍的JavaScript Date 知识浅析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

更多JavaScript Date 知识浅析相关文章请关注PHP中文网!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How do I use Java's collections framework effectively? How do I use Java's collections framework effectively? Mar 13, 2025 pm 12:28 PM

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Mar 15, 2025 am 09:19 AM

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

See all articles