尝试过:-
const dateStr = '2020-06-21T10:15:00Z',
[yyyy,mm,dd,hh,mi] = dateStr.split(/[/:-T]/)
控制台.log(
${dd}-${mm}-${yyyy} ${hh}:${mi}
)
Split 工作正常,我得到 21-06-2020 10:15。我们如何将 AM/PM 添加到此?
const dateStr = '2020-06-21T10:15:00Z';
const [yyyy, mm, dd, hh, mi] = dateStr.split(/[-:T]/);
// Convert hh to a 12-hour format and determine AM/PM
const hour12 = (hh % 12) || 12; // Ensure 12-hour format, not 0 for midnight
const ampm = hh < 12 ? 'AM' : 'PM';
// Create the formatted date string
const formattedDate = `${dd}-${mm}-${yyyy} ${hour12}:${mi} ${ampm}`;
console.log(formattedDate);