Last active
May 21, 2024 09:19
-
-
Save ramsunvtech/37e8e79e46fe08e87d7395b1f249dac6 to your computer and use it in GitHub Desktop.
JS Timestamp to Actual Date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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