JavaScript cung cấp đối tượng Ngày để làm việc với ngày và giờ, bao gồm ngày, tháng, năm, giờ, phút, giây và mili giây
Sử dụng hàm
new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
0 để lấy biểu diễn chuỗi của ngày và giờ hiện tại trong JavaScript. Sử dụng từ khóa new trong JavaScript để lấy đối tượng DateVí dụ. Ngày Trong JavaScript
Date[]; //Returns current date and time string
//or
var currentDate = new Date[]; //returns date object of current date and time
Thử nó
Tạo một đối tượng ngày bằng cách chỉ định các tham số khác nhau trong hàm tạo
new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
0Ngày [] Cú pháp
new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
Thông số
- Không có thông số. Một đối tượng ngày sẽ được đặt thành ngày & giờ hiện tại nếu không có tham số nào được chỉ định trong hàm tạo
- giá trị. Một giá trị số nguyên biểu thị số mili giây kể từ ngày 1 tháng 1 năm 1970, 00. 00. 00 UTC
- chuỗi ngày tháng. Một giá trị chuỗi sẽ được phân tích cú pháp bằng phương thức
2new Date[] new Date[value] new Date[dateString] new Date[year, monthIndex] new Date[year, monthIndex, day] new Date[year, monthIndex, day, hours] new Date[year, monthIndex, day, hours, minutes] new Date[year, monthIndex, day, hours, minutes, seconds] new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
- năm. Một giá trị số nguyên để biểu thị một năm của một ngày. Các số từ 0 đến 99 bản đồ các năm 1900 đến 1999. Tất cả những người khác là năm thực tế
- thángChỉ mục. Một giá trị số nguyên để biểu thị một tháng của một ngày. Nó bắt đầu bằng 0 cho tháng 1 đến 11 cho tháng 12
- ngày. Một giá trị số nguyên để biểu thị ngày trong tháng
- giờ. Một giá trị số nguyên để biểu thị giờ trong ngày từ 0 đến 23
- phút. Một giá trị số nguyên để biểu thị phút của một đoạn thời gian
- giây. Một giá trị số nguyên để biểu thị giây của một đoạn thời gian
- mili giây. Một giá trị số nguyên để biểu thị mili giây của một đoạn thời gian. Chỉ định số mili giây trong hàm tạo để lấy ngày và thời gian trôi qua từ ngày 1/1/1970
Trong ví dụ sau, một đối tượng ngày được tạo bằng cách chuyển mili giây trong hàm tạo
new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
0. Vì vậy, ngày sẽ được tính dựa trên mili giây trôi qua kể từ ngày 1/1/1970Ví dụ. Tạo ngày bằng cách chỉ định mili giây
var date1 = new Date[0]; // Thu Jan 01 1970 05:30:00
var date2 = new Date[1000]; // Thu Jan 01 1970 05:30:01
var date3 = new Date[5000]; // Thu Jan 01 1970 05:30:05
Thử nó
Ví dụ sau đây cho thấy các định dạng khác nhau của chuỗi ngày có thể được chỉ định trong hàm tạo
new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
0Ví dụ. Tạo ngày bằng cách chỉ định chuỗi ngày
________số 8
Thử nó
Bạn có thể sử dụng bất kỳ dấu tách hợp lệ nào trong chuỗi ngày để phân biệt các phân đoạn ngày
Ví dụ. Tạo ngày bằng dấu tách ngày khác nhau
var date1 = new Date["February 2015-3"];
var date2 = new Date["February-2015-3"];
var date3 = new Date["February-2015-3"];
var date4 = new Date["February,2015-3"];
var date5 = new Date["February,2015,3"];
var date6 = new Date["February*2015,3"];
var date7 = new Date["February$2015$3"];
var date8 = new Date["3-2-2015"]; // MM-dd-YYYY
var date9 = new Date["3/2/2015"]; // MM-dd-YYYY
Thử nó
Chỉ định bảy giá trị số để tạo một đối tượng ngày với năm, tháng đã chỉ định và tùy chọn ngày, giờ, phút, giây và mili giây
Ví dụ. Ngày tháng
Date[]; //Returns current date and time string
//or
var currentDate = new Date[]; //returns date object of current date and time
0Thử nó
Định dạng ngày
JavaScript hỗ trợ định dạng ngày theo tiêu chuẩn ISO 8601 theo mặc định -
new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
5Ví dụ. Định dạng ngày ISO
Date[]; //Returns current date and time string
//or
var currentDate = new Date[]; //returns date object of current date and time
2Chuyển đổi định dạng ngày
Sử dụng các phương thức Ngày khác nhau để chuyển đổi một ngày từ định dạng này sang định dạng khác, e. g. , sang Giờ quốc tế, GMT hoặc định dạng giờ địa phương
Ví dụ sau minh họa các phương pháp
new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
6, new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
7, new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
8 và new Date[]
new Date[value]
new Date[dateString]
new Date[year, monthIndex]
new Date[year, monthIndex, day]
new Date[year, monthIndex, day, hours]
new Date[year, monthIndex, day, hours, minutes]
new Date[year, monthIndex, day, hours, minutes, seconds]
new Date[year, monthIndex, day, hours, minutes, seconds, milliseconds]
9 để chuyển đổi ngày thành các định dạng tương ứngVí dụ. Chuyển đổi ngày ở các định dạng khác nhau
Date[]; //Returns current date and time string
//or
var currentDate = new Date[]; //returns date object of current date and time
7Thử nó
Để nhận chuỗi ngày ở các định dạng khác với các định dạng được liệt kê ở trên, bạn cần tạo chuỗi ngày theo cách thủ công bằng các phương thức đối tượng ngày khác nhau. Ví dụ sau chuyển đổi chuỗi ngày thành định dạng DD-MM-YYYY
Ví dụ. Nhận phân đoạn ngày
Date[]; //Returns current date and time string
//or
var currentDate = new Date[]; //returns date object of current date and time
8Thử nó
Ghi chú
Sử dụng thư viện Ngày JavaScript của bên thứ ba như datejs. com hoặc khoảnh khắc. com để hoạt động rộng rãi với Ngày trong JavaScript