Summary of JavaScript Date object usage_javascript tips
//全局函数
Date
//Date 类的静态方法
Date.parse
Date.UTC
//Date 对象的建立方法
new Date()
new Date(毫秒数)
new Date(标准时间格式字符串)
new Date(年, 月, 日, 时, 分, 秒, 毫秒)
//Date 对象的更多方法
getFullYear (getUTCFullYear)
getMonth (getUTCMonth)
getDate (getUTCDate)
getDay (getUTCDay)
getHours (getUTCHours)
getMinutes (getUTCMinutes)
getSeconds (getUTCSeconds)
getMilliseconds (getUTCMilliseconds)
getTime
getTimezoneOffset
setFullYear (setUTCFullYear)
setMonth (setUTCMonth)
setDate (setUTCDate)
setHours (setUTCHours)
setMinutes (setUTCMinutes)
setSeconds (setUTCSeconds)
setMilliseconds (setUTCMilliseconds)
setTime
toDateString
toTimeString
toUTCString
toLocaleString
toLocaleDateString
toLocaleTimeString
toString
valueOf
--------------------------------------------------------------------------------
根据一个或多个数值建立时间对象, 及本地计时与世界标准计时的区别
--------------------------------------------------------------------------------
//先用最容易理解的方式建立一个时间对象
var d = new Date(2009, 2, 27, 12, 59, 59, 999);
alert(d); //Fri Mar 27 12:59:59 UTC+0800 2009
alert(d.toString()); //Fri Mar 27 12:59:59 UTC+0800 2009
alert(d.toUTCString()); //Fri, 27 Mar 2009 04:59:59 UTC
alert(d.toLocaleString()); //2009年3月27日 12:59:59
alert(d.toDateString()); //Fri Mar 27 2009
alert(d.toLocaleDateString()); //2009年3月27日
alert(d.toTimeString()); //12:59:59 UTC+0800
alert(d.toLocaleTimeString()); //12:59:59
/* 时间在计算机中被记为一个整数, 这是从 UTC 时间的 1970-1-1 0:0:0 到此时间的毫秒数 */
alert(d.valueOf()); //1238129999999
alert(d.getTime()); //1238129999999
/* 获取本地时间和世界标准计时的时差 */
alert(d.getTimezoneOffset()); //-480; 单位是分钟, 也就是 8 小时
/* 直接使用时间值(毫秒数, 譬如上面的: 1238129999999)建立时间对象 */
var d = new Date(1238129999999);
alert(d.toLocaleString()); //2009年3月27日 12:59:59
/* 但建立函数有 2-7 个参数时, 将是根据 "年, 月, 日, 时, 分, 秒, 毫秒" 建立时间 */
d = new Date(2009, 2, 27, 12, 59, 59, 999);
alert(d.toLocaleString()); //2009年3月27日 12:59:59
d = new Date(2009, 2, 27, 12, 59, 59);
alert(d.toLocaleString()); //2009年3月27日 12:59:59
d = new Date(2009, 2, 27, 12, 59);
alert(d.toLocaleString()); //2009年3月27日 12:59:00
d = new Date(2009, 2, 27, 12);
alert(d.toLocaleString()); //2009年3月27日 12:00:00
d = new Date(2009, 2, 27);
alert(d.toLocaleString()); //2009年3月27日 0:00:00
d = new Date(2009, 2);
alert(d.toLocaleString()); //2009年3月1日 0:00:00
/* Date 类的静态函数 UTC 的参数也是和上面一样的 2-7 个, 但 UTC 获取的是个 number */
var n = Date.UTC(2009, 0); //这只是获取了那个表示时间的毫秒数
alert(typeof n); //number
var d = new Date(n); //根据刚刚获取的数、重新建立为时间对象
alert(d.toUTCString()); //Thu, 1 Jan 2009 00:00:00 UTC
alert(d.toLocaleString()); //2009年1月1日 8:00:00
--------------------------------------------------------------------------------
无参数建立的时间对象、和用全局函数 Date 获取的时间的区别
--------------------------------------------------------------------------------
var d1 = new Date(); //返回时间对象
var d2 = Date(); //返回时间字符串
alert(d1); //Fri Feb 27 13:20:58 UTC+0800 2009
alert(d2); //Fri Feb 27 13:20:58 2009
alert(d1.valueOf()); //1235712058340
alert(d2.valueOf()); //Fri Feb 27 13:20:58 2009
alert(typeof d1); //object
alert(typeof d2); //string
//明显看出 d2 只是字符串, 它可以使用 String 类的方法, 而不能使用 Date 类的方法.
--------------------------------------------------------------------------------
使用字符串参数建立时间对象
--------------------------------------------------------------------------------
var d;
d = new Date('Fri Mar 27 12:59:59 UTC+0800 2009');
alert(d.toLocaleString()); //2009年3月27日 12:59:59
d = new Date('Fri Mar 27 12:59:59 2009');
alert(d.toLocaleString()); //2009年3月27日 12:59:59
d = new Date('Fri Mar 27 2009');
alert(d.toLocaleString()); //2009年3月27日 0:00:00
d = new Date('Mar 27 2009');
alert(d.toLocaleString()); //2009年3月27日 0:00:00
/* 可使用 Date() 返回的字符串 */
var ts = Date();
d = new Date(ts);
alert(d.toLocaleString()); //2009年3月27日 14:04:38
/* Date 类的静态函数 parse 也是需要一样的字符参数, 不过它返回的是个数字(那个毫秒数) */
var n;
n = Date.parse('Mar 27 2009');
alert(n); //1238083200000
alert(typeof n); //number
d = new Date(n);
alert(d.toLocaleString()); //March 27, 2009 0:00:00
------------------------- -------------------------------------------------- -----
Get and set respectively: year, month, day, hour, minute, second, millisecond, among which "day of week" can be obtained but cannot be set
---------- -------------------------------------------------- --------------------
var d = new Date(2009, 2, 27, 12, 58, 59, 999);
alert(d .toLocaleString()); //March 27, 2009 0:00:00
alert(d.getFullYear()); //2009
alert(d.getMonth()); //2 ( Starting from 0, 2 refers to March)
alert(d.getDate()); //27
alert(d.getDay()); //5 (Friday)
alert(d.getHours ()); //12
alert(d.getMinutes()); //58
alert(d.getSeconds()); //59
alert(d.getMilliseconds()); / /999 (milliseconds)
d.setFullYear(2010);
d.setMonth(1);
d.setDate(2);
d.setHours(3);
d. setMinutes(4);
d.setSeconds(5);
d.setMilliseconds(6);
alert(d.toLocaleString()); //February 2, 2010 3:04:05
alert(d.getFullYear()); //2010
alert(d.getMonth()); //1 (starting from 0, 1 refers to February)
alert(d.getDate() ); //2
alert(d.getDay()); //2 (Tuesday)
alert(d.getHours()); //3
alert(d.getMinutes()); //4
alert(d.getSeconds()); //5
alert(d.getMilliseconds()); //6 (milliseconds)
/* There is also a setTime */
var d = new Date();
d.setTime(0);
alert(d.toUTCString()); //Thu, 1 Jan 1970 00:00:00 UTC

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
