Hy vọng rằng câu trả lời này dễ đọc hơn một chút so với các câu trả lời khác [đặc biệt là đối với những người mới đến].
Đây là giải pháp tôi đã triển khai trong một số trang web của tôi để thông báo lần cuối cùng mã trang web được sửa đổi. Nó thực hiện thời gian AM/pm thông qua tham số options
của date.toLocaleDateString
[xem tài liệu Mozilla liên quan].
// Last time page code was updated/changed
const options = {
year: "numeric",
month: "long",
weekday: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: true // This is the line of code we care about here
/*
false: displays 24hs format for time
true: displays 12, AM and PM format
*/
};
let last = document.lastModified;
let date = new Date[last];
let local = date.toLocaleDateString["en-US", options];
let fullDate = `${local}`;
document.getElementById["updated"].textContent = fullDate;
Đầu ra nào ở định dạng:
Saturday, May 28, 2022, 8:38:50 PM
Đầu ra này sau đó được hiển thị trong mã HTML sau:
Last update: _update_date_goes_here
Lưu ý: Trong trường hợp sử dụng này, document.lastModified
có một số hành vi kỳ lạ tùy thuộc vào việc chạy cục bộ hay trên máy chủ bên ngoài [xem câu hỏi tràn ngăn xếp này]. Mặc dù nó hoạt động chính xác khi tôi chạy nó trong trang GitHub của tôi [bạn sẽ thấy nó hoạt động trong trang web ở chân trang].
Định dạng thời gian để am pm javascript với các ví dụ mã Trong bài đăng này, chúng tôi sẽ kiểm tra cách giải quyết thời gian định dạng để AM JavaScript vấn đề bằng cách sử dụng các ví dụ từ ngôn ngữ lập trình. Dòng mã sau đây phác thảo các phương pháp khác nhau có thể được sử dụng để tìm giải pháp cho thời gian định dạng cho AM JavaScript vấn đề. Sử dụng nhiều ví dụ, chúng tôi đã học được cách giải quyết thời gian định dạng để giải JavaScript.const formatAMPM = [date] => {
let hours = date.getHours[];
let minutes = date.getMinutes[];
const ampm = hours >= 12 ? 'pm' : 'am';
hours %= 12;
hours = hours || 12;
minutes = minutes < 10 ? `0${minutes}` : minutes;
const strTime = `${hours}:${minutes} ${ampm}`;
return strTime;
};
console.log[formatAMPM[new Date[]]];
var suffix = hour >= 12 ? "PM":"AM";
var hours = [[hour + 11] % 12 + 1] + suffix
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
In this approach, we will change the DateTime format by only using native methods. Simply put, we will apply modulo “%” operator to find the hour in 12-hour format and use conditional “?:” operator to apply “AM” or “PM”.
Program:
Saturday, May 28, 2022, 8:38:50 PM
0Saturday, May 28, 2022, 8:38:50 PM
1Saturday, May 28, 2022, 8:38:50 PM
2JavaScript sử dụng định dạng 24 giờ làm mặc định cho DateTime. Tuy nhiên, ban ngày trong JavaScript có thể được hiển thị ở định dạng 12 giờ AM/PM bằng cách sử dụng một số phương pháp. Chúng tôi sẽ xem xét một cặp vợ chồng trong bài viết này.
Saturday, May 28, 2022, 8:38:50 PM
6Saturday, May 28, 2022, 8:38:50 PM
0Saturday, May 28, 2022, 8:38:50 PM
8Saturday, May 28, 2022, 8:38:50 PM
2Cách tiếp cận 1: Theo cách tiếp cận này, chúng tôi sẽ thay đổi định dạng DateTime bằng cách chỉ sử dụng các phương thức gốc. Nói một cách đơn giản, chúng tôi sẽ áp dụng nhà điều hành modulo,%, để tìm giờ ở định dạng 12 giờ và sử dụng điều kiện?
Last update: _update_date_goes_here
7Last update: _update_date_goes_here
8Last update: _update_date_goes_here
9const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];0
Last update: _update_date_goes_here
2Saturday, May 28, 2022, 8:38:50 PM
2Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
0const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];5
Saturday, May 28, 2022, 8:38:50 PM
2Last update: _update_date_goes_here
7const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];8
Last update: _update_date_goes_here
7var suffix = hour >= 12 ? "PM":"AM"; var hours = [[hour + 11] % 12 + 1] + suffix0
Last update: _update_date_goes_here
9const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];0
const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];5
Saturday, May 28, 2022, 8:38:50 PM
2Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
0___Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
0dateObject.toLocaleString[[locales[, options]]]6
Saturday, May 28, 2022, 8:38:50 PM
2dateObject.toLocaleString[[locales[, options]]]8
dateObject.toLocaleString[[locales[, options]]]9
dateObject.toLocaleString[[locales[, options]]]8
options
1dateObject.toLocaleString[[locales[, options]]]8
options
3dateObject.toLocaleString[[locales[, options]]]8
options
5options
6 options
7options
8options
9
dateObject.toLocaleString[[locales[, options]]]8
date.toLocaleDateString
1options
8date.toLocaleDateString
3
dateObject.toLocaleString[[locales[, options]]]8
date.toLocaleDateString
5___dateObject.toLocaleString[[locales[, options]]]8
document.lastModified
1 date.toLocaleDateString
77____83 document.lastModified
4Last update: _update_date_goes_here
0document.lastModified
6Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
0document.lastModified
9
0Last update: _update_date_goes_here
4
2Last update: _update_date_goes_here
7
5Last update: _update_date_goes_here
9const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];0
document.lastModified
9Saturday, May 28, 2022, 8:38:50 PM
2Các
Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
0dateObject.toLocaleString[[locales[, options]]]6
Saturday, May 28, 2022, 8:38:50 PM
2dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
14dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
16
dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
18options
8options
3
options
8
Saturday, May 28, 2022, 8:38:50 PM
22options
8
Saturday, May 28, 2022, 8:38:50 PM
24options
8
Saturday, May 28, 2022, 8:38:50 PM
26options
8
Saturday, May 28, 2022, 8:38:50 PM
28options
8
Saturday, May 28, 2022, 8:38:50 PM
30options
8
Saturday, May 28, 2022, 8:38:50 PM
32options
8
Saturday, May 28, 2022, 8:38:50 PM
34options
8
Saturday, May 28, 2022, 8:38:50 PM
36options
8
Saturday, May 28, 2022, 8:38:50 PM
38options
6 Saturday, May 28, 2022, 8:38:50 PM
40options
8
Saturday, May 28, 2022, 8:38:50 PM
42Saturday, May 28, 2022, 8:38:50 PM
43 Saturday, May 28, 2022, 8:38:50 PM
44Saturday, May 28, 2022, 8:38:50 PM
45document.lastModified
3 Saturday, May 28, 2022, 8:38:50 PM
47dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
49Last update: _update_date_goes_here
0document.lastModified
6Saturday, May 28, 2022, 8:38:50 PM
52const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];0
Saturday, May 28, 2022, 8:38:50 PM
8Saturday, May 28, 2022, 8:38:50 PM
2const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];0
Saturday, May 28, 2022, 8:38:50 PM
4Saturday, May 28, 2022, 8:38:50 PM
2const formatAMPM = [date] => { let hours = date.getHours[]; let minutes = date.getMinutes[]; const ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours || 12; minutes = minutes < 10 ? `0${minutes}` : minutes; const strTime = `${hours}:${minutes} ${ampm}`; return strTime; }; console.log[formatAMPM[new Date[]]];0
Saturday, May 28, 2022, 8:38:50 PM
1Saturday, May 28, 2022, 8:38:50 PM
2Before:
After:
Cách tiếp cận 2: Theo cách tiếp cận này, chúng tôi sẽ sử dụng một phương thức sẵn có Tolocalestring [] để thay đổi định dạng của ngày đã cho thành định dạng AM-PM.
In this approach, we will utilize an inbuilt method toLocaleString[] to change the format of given
date into AM-PM format.
tolocalestring []: nó trả về một biểu diễn chuỗi của đối tượng ngày. 2 đối số địa phương và các tùy chọn cho phép tùy chỉnh hành vi của phương thức. It returns a string representation of the date Object. The 2 arguments Locale and options allow for customization of the behavior of the method.
Syntax:
dateObject.toLocaleString[[locales[, options]]]
Program:
Saturday, May 28, 2022, 8:38:50 PM
63Saturday, May 28, 2022, 8:38:50 PM
64Saturday, May 28, 2022, 8:38:50 PM
6Saturday, May 28, 2022, 8:38:50 PM
66Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
68Last update: _update_date_goes_here
5Saturday, May 28, 2022, 8:38:50 PM
70Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
72Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
74dateObject.toLocaleString[[locales[, options]]]0
Saturday, May 28, 2022, 8:38:50 PM
76
Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
78dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
80 Saturday, May 28, 2022, 8:38:50 PM
81Saturday, May 28, 2022, 8:38:50 PM
82 Saturday, May 28, 2022, 8:38:50 PM
83dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
80 Saturday, May 28, 2022, 8:38:50 PM
86Saturday, May 28, 2022, 8:38:50 PM
87 Saturday, May 28, 2022, 8:38:50 PM
88Saturday, May 28, 2022, 8:38:50 PM
89Saturday, May 28, 2022, 8:38:50 PM
90options
8options
9
dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
80 Saturday, May 28, 2022, 8:38:50 PM
95Saturday, May 28, 2022, 8:38:50 PM
87 Saturday, May 28, 2022, 8:38:50 PM
88Saturday, May 28, 2022, 8:38:50 PM
89Saturday, May 28, 2022, 8:38:50 PM
99options
8date.toLocaleDateString
3
dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
80 Last update: _update_date_goes_here
04Last update: _update_date_goes_here
05Last update: _update_date_goes_here
06dateObject.toLocaleString[[locales[, options]]]8
Last update: _update_date_goes_here
08Last update: _update_date_goes_here
09 Last update: _update_date_goes_here
10Last update: _update_date_goes_here
0document.lastModified
6Last update: _update_date_goes_here
0Last update: _update_date_goes_here
14
2Saturday, May 28, 2022, 8:38:50 PM
2Last update: _update_date_goes_here
7
5Last update: _update_date_goes_here
9Last update: _update_date_goes_here
20Last update: _update_date_goes_here
0Last update: _update_date_goes_here
22Saturday, May 28, 2022, 8:38:50 PM
05Last update: _update_date_goes_here
24Last update: _update_date_goes_here
0Saturday, May 28, 2022, 8:38:50 PM
78dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
80 Last update: _update_date_goes_here
29Saturday, May 28, 2022, 8:38:50 PM
82 Saturday, May 28, 2022, 8:38:50 PM
83dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
80 Last update: _update_date_goes_here
34dateObject.toLocaleString[[locales[, options]]]8
Last update: _update_date_goes_here
36 Last update: _update_date_goes_here
37options
8
Saturday, May 28, 2022, 8:38:50 PM
80 Last update: _update_date_goes_here
40Saturday, May 28, 2022, 8:38:50 PM
82 Saturday, May 28, 2022, 8:38:50 PM
83options
8
Saturday, May 28, 2022, 8:38:50 PM
80 Last update: _update_date_goes_here
45Last update: _update_date_goes_here
46Last update: _update_date_goes_here
47Last update: _update_date_goes_here
48Last update: _update_date_goes_here
49Last update: _update_date_goes_here
46Last update: _update_date_goes_here
51Last update: _update_date_goes_here
48options
8
Last update: _update_date_goes_here
54options
8
Last update: _update_date_goes_here
56Saturday, May 28, 2022, 8:38:50 PM
05Last update: _update_date_goes_here
58dateObject.toLocaleString[[locales[, options]]]8
Saturday, May 28, 2022, 8:38:50 PM
49Last update: _update_date_goes_here
0document.lastModified
6Saturday, May 28, 2022, 8:38:50 PM
52Last update: _update_date_goes_here
64Last update: _update_date_goes_here
65Last update: _update_date_goes_here
66Before:
After: