Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday 9 July 2015

In this article I will explain how to set different date format in Javascript. We can display date in short or long format in Javascript.
In this article we use three Label Controls to display different date formats.  We get these controls in script under Head section and set values of different format to these controls.


Following Javascript We use to display different formats:

<script type="text/javascript">
    function getdates() {
        var today = new Date()
        document.getElementById('lblDefault').innerHTML = today;
        document.getElementById('lblPrev').innerHTML = [today.getDate(), today.getMonth() + 1, today.getFullYear()].join('/');
        document.getElementById('lbldateformat').innerHTML = [today.getDate(), today.getMonth() + 1, today.getFullYear()].join('/') + ' ' + [today.getHours(), today.getMinutes(), today.getSeconds()].join(':');
    }
</script>

Source Code:
Full Source code of Sample Application:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Set Different Format In JavaScript</title>
<script type="text/javascript">
    function getdates() {
        var today = new Date()
        document.getElementById('lblDefault').innerHTML = today;
        document.getElementById('lblPrev').innerHTML = [today.getDate(), today.getMonth() + 1, today.getFullYear()].join('/');
        document.getElementById('lbldateformat').innerHTML = [today.getDate(), today.getMonth() + 1, today.getFullYear()].join('/') + ' ' + [today.getHours(), today.getMinutes(), today.getSeconds()].join(':');
    }
</script>
</head>
<body onload="getdates()">
<form id="form1">
<div>
<b>Default Date Format:</b>
<label id="lblDefault" />
</div>
<div>
<b>Date in dd mm yyyy format:</b>
<label id="lblPrev" />
</div>
<div>
<b>Date in dd mm yyyy hh:mm:ss Format:</b>
<label id="lbldateformat" />
</div>
</form>
</body>
</html>


Use this code and check result. We can use this code in our applications.
Categories: ,

0 comments:

Post a Comment