Skip to content

Instantly share code, notes, and snippets.

@ramsunvtech
Last active May 21, 2024 09:19
Show Gist options
  • Save ramsunvtech/37e8e79e46fe08e87d7395b1f249dac6 to your computer and use it in GitHub Desktop.
Save ramsunvtech/37e8e79e46fe08e87d7395b1f249dac6 to your computer and use it in GitHub Desktop.
JS Timestamp to Actual Date
function formatTimestamp(timestampInput) {
const timestamp = parseInt(timestampInput, 10);
if (isNaN(timestamp)) {
return 'Invalid timestamp';
}
// Convert timestamp from seconds to milliseconds
const date = new Date(timestamp * 1000);
const options = { day: '2-digit', month: 'short', year: 'numeric' };
const formattedDate = date.toLocaleDateString('en-GB', options).replace(/ /g, '/').replace(/,/g, '');
return formattedDate;
}
// formatTimestamp(1716283066);
// should return '21/May/2024'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment