Detailed introduction to the usage of data objects in js (with code)
This article brings you a detailed introduction to the usage of data objects in js (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Introduction
Handling dates in JavaScript can be complex and often a pain for developers regardless of their skills.
JavaScript provides us with date processing functions through a powerful Date
object.
DATE Object
Date
object instance represents a single point in time.
Although named Date
, it is also used Processing time.
Initialize Date object
We initialize a Date object through the following code:
1 |
|
The above code creates a date object that represents the current moment.
Internally, the date represents the number of milliseconds since January 1, 1970 (UTC). This time is important because as far as the computer is concerned, this is when it started.
You may be familiar with UNIX timestamps: this represents the number of seconds that have elapsed since that famous date.
Note that UNIX timestamps are in seconds and JavaScript dates are in milliseconds
If we have a UNIX timestamp, we can initialize a JavaScript date object through the following method:
1 2 |
|
If we pass in 0, we will get the date representing Jan 1st 1970 (UTC).
1 |
|
If we pass in a string instead of a value, the Date object will use the parse
method to determine which date you pass in, such as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
It’s very flexible here. You can add or omit leading zeros within months or days.
You need to pay attention to the position of month/day, otherwise the month may be parsed as a date.
Use Date.parse
You can also process strings:
1 2 3 4 5 6 7 8 9 10 |
|
Date.parse
will return a timestamp expressed in milliseconds instead of a Date object
You can also pass in values in order to represent each part of the date. The parameter order is as follows: year, month (starting from 0), date, hour, minute, second, millisecond
1 2 |
|
At least three parameters need to be passed in, but most JavaScript engines can also parse less than three parameters
1 2 |
|
The final result of the above code is a relative value that depends on your computer's time zone. This means that passing the same parameters may produce different results on different computers.
JavaScript, without any information about the time zone, treats the date as UTC and the result is automatically converted for the current computer time zone.
To summarize, there are four methods that allow you to create a new Date object:
-
If no parameters are passed, a Date object will be created based on the current time;
Pass in a value representing the number of milliseconds in the past since 1 Jan 1970 00:00 GMT;
-
Pass in a string representing the date;
Pass in a series of parameters that represent each item;
Time zone
When initializing the date, you can also pass in the time zone, At this point the date is not assumed to be UTC and is then converted to the local time zone.
The time zone can be passed in HPURS format or by adding a time zone name.
1 2 |
|
If an incorrect time zone name is passed in during parsing, JavaScript will use UTC by default and will not report an error.
If a value in the wrong format is passed in, JavaScript will report an Invaild Date
error.
Date conversion and formatting
For a given date object, there are many methods to produce a string based on the date
1 2 3 4 5 6 7 8 9 10 11 |
|
GETTER method of Date object
The Date object provides several methods for checking its value. The results of these methods all depend on the current time zone of the computer
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The above methods have corresponding versions for obtaining UTC time:
1 2 3 4 5 6 7 8 |
|
Editing the Date object
The Date object provides Several editing date value methods
1 2 3 4 5 6 7 8 9 10 11 12 |
|
setDay
andsetMonth
all start processing from the value 0, for example, March should be the value 2
Here is one Trivia: These methods "overlap", so for example if you use date.setHours (48)
, the result will affect the day.
There is another piece of trivia: you can pass in multiple parameters to the setHours()
method to set minutes, seconds, and milliseconds, such as setHours(0, 0, 0, 0) Similar situations exist for
, setMinutes
and setSeconds
.
Similar to many methods of getting dates, there are also methods of setting dates for the UTC version:
1 2 3 4 5 6 7 8 9 10 |
|
Get the current timestamp
If you want to get the milliseconds The current timestamp in units, it is recommended to use the following method:
1 |
|
instead of
1 |
|
JavaScript always try to get the most accurate results
As mentioned above, you The number of days passed in will affect the total date. This will not report an error and will directly update the month.
1 |
|
The above phenomenon also takes effect on dates, hours, minutes, seconds and milliseconds
依据本地情况格式化日期
Internationalization API 在现代浏览器中有很好的支持(除了 UC浏览器),允许你转换日期。
本地化方法通过,通过 Int1
对象暴露,这个对象还可以用来帮助本地化数值,字符串以及货币。
这里我们用到的是 Intl.DateTimeFormat()
我们可以通过下述方法来依据电脑的本地情况格式化一个日期:
1 2 |
|
也可以依据不同的时区格式化日期:
1 |
|
Intl.DateTimeFormat
方法还接收一个可选的参数用以自定义输出格式,可以用来展示 小时,分钟和秒
1 2 3 4 5 6 7 8 9 10 11 |
|
点击这个链接可以查看所有可以用到的属性
两个日期的对比
可以通过 Date.getTime()
获取两个日期之间的差别
1 2 3 |
|
同样也可以通过这个方法检测两个日期是否相同:
1 2 3 4 |
|
需要注意的是,getTime()
方法比较的是毫秒,所以 July 10, 2018 07:22:13
和 July 10, 2018
并不相等。不过你可以通过 setHours(0, 0, 0, 0)
来重置时间。
相关推荐:
js data日期初始化的5种方法_javascript技巧
javascript-问:php使用post方式提交data,进行js加密,然后显示出来
The above is the detailed content of Detailed introduction to the usage of data objects in js (with code). For more information, please follow other related articles on the PHP Chinese website!

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
