Чтобы добавить AM/PM к метке времени в React, вы можете использовать различные методы. Вот несколько подходов:
Метод 1: использование метода JavaScript toLocaleTimeString
const timestamp = new Date(); // Replace this with your timestamp
const formattedTime = timestamp.toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true });
// Example usage in JSX
return <div>{formattedTime}</div>;
Метод 2: использование библиотеки Moment.js
import moment from 'moment';
const timestamp = new Date(); // Replace this with your timestamp
const formattedTime = moment(timestamp).format('h:mm A');
// Example usage in JSX
return <div>{formattedTime}</div>;
Метод 3. Использование API Intl.DateTimeFormat
const timestamp = new Date(); // Replace this with your timestamp
const formattedTime = new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }).format(timestamp);
// Example usage in JSX
return <div>{formattedTime}</div>;
Метод 4. Использование простого JavaScript (конвертация часов)
const timestamp = new Date(); // Replace this with your timestamp
const hours = timestamp.getHours();
const minutes = timestamp.getMinutes();
const period = hours >= 12 ? 'PM' : 'AM';
const formattedTime = `${(hours % 12) || 12}:${minutes.toString().padStart(2, '0')} ${period}`;
// Example usage in JSX
return <div>{formattedTime}</div>;