Javascript – Date

Đối tượng Date là một kiểu dữ liệu được tích hợp trong ngôn ngữ JavaScript. Các đối tượng ngày được tạo với Date() mới như hình bên dưới.

Khi một đối tượng Date được tạo, một số phương thức cho phép bạn thao tác trên nó. Hầu hết các phương pháp chỉ cho phép bạn lấy và đặt các trường năm, tháng, ngày, giờ, phút, giây và mili giây của đối tượng, sử dụng giờ địa phương hoặc giờ UTC (phổ quát hoặc GMT).

Tiêu chuẩn ECMAScript yêu cầu đối tượng Date có thể biểu thị bất kỳ ngày và giờ nào, với độ chính xác đến mili giây, trong vòng 100 triệu ngày trước hoặc sau ngày 1/1/1970. Đây là phạm vi cộng hoặc trừ 273.785 năm, vì vậy JavaScript có thể biểu thị ngày và giờ cho đến năm 275755.

Cú pháp

Bạn có thể sử dụng bất kỳ cú pháp nào sau đây để tạo một đối tượng Date bằng hàm tạo Date().

new Date( )
new Date(milliseconds)
new Date(datestring)
new Date(year,month,date[,hour,minute,second,millisecond ])

Lưu ý – Các tham số trong ngoặc luôn là tùy chọn.

Dưới đây là mô tả về các tham số –

     Không có đối số – Không có đối số, hàm tạo Date() tạo đối tượng Ngày được đặt thành ngày và giờ hiện tại.

     mili giây − Khi một đối số dạng số được truyền, nó được coi là biểu diễn số bên trong của ngày tính bằng mili giây, như được trả về bởi phương thức getTime(). Ví dụ: chuyển đối số 5000 sẽ tạo một ngày đại diện cho 5 giây sau nửa đêm ngày 1/1/70.

     datestring − Khi một đối số chuỗi được truyền, nó là một chuỗi đại diện cho một ngày, ở định dạng được chấp nhận bởi phương thức Date.parse().

     7 agruments − Để sử dụng dạng cuối cùng của hàm tạo được hiển thị ở trên. Dưới đây là mô tả của từng đối số –

         năm − Giá trị số nguyên đại diện cho năm. Để tương thích (để tránh sự cố Y2K), bạn phải luôn chỉ định năm đầy đủ; sử dụng 1998, thay vì 98.

         tháng − Giá trị số nguyên đại diện cho tháng, bắt đầu bằng 0 cho tháng 1 đến 11 cho tháng 12.

         date − Giá trị số nguyên đại diện cho ngày trong tháng.

         giờ – Giá trị số nguyên biểu thị giờ trong ngày (thang đo 24 giờ).

         phút – Giá trị số nguyên biểu thị phân đoạn phút của thời gian đọc.

         thứ hai – Giá trị số nguyên đại diện cho phân đoạn thứ hai của việc đọc thời gian.

         mili giây – Giá trị số nguyên biểu thị phân đoạn mili giây của thời gian đọc.

Thuộc tính Date

Here is a list of the properties of the Date object along with their description.

Sr.No.Property & Description
1constructor Specifies the function that creates an object’s prototype.
2prototype The prototype property allows you to add properties and methods to an object

Phương thức Date

Sr.No.Method & Description
1Date() Returns today’s date and time
2getDate() Returns the day of the month for the specified date according to local time.
3getDay() Returns the day of the week for the specified date according to local time.
4getFullYear() Returns the year of the specified date according to local time.
5getHours() Returns the hour in the specified date according to local time.
6getMilliseconds() Returns the milliseconds in the specified date according to local time.
7getMinutes() Returns the minutes in the specified date according to local time.
8getMonth() Returns the month in the specified date according to local time.
9getSeconds() Returns the seconds in the specified date according to local time.
10getTime() Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
11getTimezoneOffset() Returns the time-zone offset in minutes for the current locale.
12getUTCDate() Returns the day (date) of the month in the specified date according to universal time.
13getUTCDay() Returns the day of the week in the specified date according to universal time.
14getUTCFullYear() Returns the year in the specified date according to universal time.
15getUTCHours() Returns the hours in the specified date according to universal time.
16getUTCMilliseconds() Returns the milliseconds in the specified date according to universal time.
17getUTCMinutes() Returns the minutes in the specified date according to universal time.
18getUTCMonth() Returns the month in the specified date according to universal time.
19getUTCSeconds() Returns the seconds in the specified date according to universal time.
20getYear() Deprecated – Returns the year in the specified date according to local time. Use getFullYear instead.
21setDate() Sets the day of the month for a specified date according to local time.
22setFullYear() Sets the full year for a specified date according to local time.
23setHours() Sets the hours for a specified date according to local time.
24setMilliseconds() Sets the milliseconds for a specified date according to local time.
25setMinutes() Sets the minutes for a specified date according to local time.
26setMonth() Sets the month for a specified date according to local time.
27setSeconds() Sets the seconds for a specified date according to local time.
28setTime() Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
29setUTCDate() Sets the day of the month for a specified date according to universal time.
30setUTCFullYear() Sets the full year for a specified date according to universal time.
31setUTCHours() Sets the hour for a specified date according to universal time.
32setUTCMilliseconds() Sets the milliseconds for a specified date according to universal time.
33setUTCMinutes() Sets the minutes for a specified date according to universal time.
34setUTCMonth() Sets the month for a specified date according to universal time.
35setUTCSeconds() Sets the seconds for a specified date according to universal time.
36setYear() Deprecated – Sets the year for a specified date according to local time. Use setFullYear instead.
37toDateString() Returns the “date” portion of the Date as a human-readable string.
38toGMTString() Deprecated – Converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.
39toLocaleDateString() Returns the “date” portion of the Date as a string, using the current locale’s conventions.
40toLocaleFormat() Converts a date to a string, using a format string.
41toLocaleString() Converts a date to a string, using the current locale’s conventions.
42toLocaleTimeString() Returns the “time” portion of the Date as a string, using the current locale’s conventions.
43toSource() Returns a string representing the source for an equivalent Date object; you can use this value to create a new object.
44toString() Returns a string representing the specified Date object.
45toTimeString() Returns the “time” portion of the Date as a human-readable string.
46toUTCString() Converts a date to a string, using the universal time convention.
47valueOf() Returns the primitive value of a Date object.

Phương thức tĩnh Date

Ngoài nhiều phương thức thể hiện được liệt kê trước đó, đối tượng Date cũng định nghĩa hai phương thức tĩnh. Các phương thức này được gọi thông qua hàm tạo Date().

1Date.parse( ) Parses a string representation of a date and time and returns the internal millisecond representation of that date.
2Date.UTC( ) Returns the millisecond representation of the specified UTC date and time.