function formatTimestamp(timestamp) {
// Convert timestamp to a Date object if it's not already
const date = new Date(timestamp);
// Format the date based on the desired output
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Month is 0-indexed
const day = String(date.getDate()).padStart(2, '0');
const hour = String(date.getHours()).padStart(2, '0');
const minute = String(date.getMinutes()).padStart(2, '0');
const second = String(date.getSeconds()).padStart(2, '0');
// Return the formatted timestamp
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
//Example Usage (optional - remove for production)
//const timestamp = 1678886400000; // Example timestamp in milliseconds
//const formattedTimestamp = formatTimestamp(timestamp);
//console.log(formattedTimestamp);
Add your comment