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

Add your comment