JavaScript Date Format



<!DOCTYPE html>
<html>
<head>
<title>DateTime</title>
<style type="text/css">
*{
font-family: arial;
}
p{
background-color: red;
color: white;
padding: 5px;
}
</style>
</head>
<p id='date1'>Loading...</p>
<body>
<script type="text/javascript">
/************************************ Start Date And Time ***/
function dateFormat(dateVal = ''){
var now = dateVal!='' ? new Date(dateVal) : new Date();
var arrDate = [], arrTime = [];
arrDate.push(now.getFullYear());
arrDate.push(String(now.getMonth()).padStart(2,0));
arrDate.push(String(now.getDate()).padStart(2,0));
arrTime.push(String(now.getHours()).padStart(2,0));
arrTime.push(String(now.getMinutes()).padStart(2,0));
arrTime.push(String(now.getSeconds()).padStart(2,0));
return arrDate.join('-') + ' ' + arrTime.join(':');
}
document.getElementById('date1').innerHTML = dateFormat();// '2018-09-25 10:00:00'
/************************************ End Date And Time ***/
</script>
</body>
</html>

--------------------------------------------------------------------------------------

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment