In this video tutorial, you will learn how to convert timestamp to date format in javascript.
Subscribe : https://www.youtube.com/channel/UCBwPlFFaigg5WA-Y1Iz-HUA
Source Code:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div>
<button>Get</button>
<h1>Result</h1>
</div>
<script src="script.js"></script>
</body>
</html>
CSS:
body {
text-align: center;
}
div {
display: inline-block;
}
button {
display: inline-block;
padding: 10px 20px;
}
Javascript:
let btnGet = document.querySelector('button');
let result = document.querySelector('h1');
//1/5/2020
let timestamp = 1578162600000;
btnGet.addEventListener('click', () => {
let dateObj = new Date(timestamp);
let month = dateObj.getMonth() + 1;
let year = dateObj.getFullYear();
let date = dateObj.getDate();
result.innerText = `${month}/${date}/${year}`;
});
#js #javascript