To format a JavaScript date

In this tutorial learn how to format JavaScript date.

EXAMPLE 1



<html>
<button onclick="myFunction()">
Click me
</button>
<script>
function myFunction() {
var d = new Date();
var datestring = d.getDate()  + "-" + (d.getMonth()+1) + "-" + d.getFullYear() + " " +
d.getHours() + ":" + d.getMinutes();
alert(datestring);
}
</script>
</html>
The above example output: 23-9-2019 22:47

EXAMPLE 2

<html>
<button onclick="myFunction()">
Click me
</button>
<script>
function myFunction() {
var D = new Date();
var now = new Date()
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
var formattedDate = now.getDate() + "-" + months[now.getMonth()] + "-" + now.getFullYear()
alert(formattedDate)
}
</script>

</html>

The output of above example : 23-Sep-2019



No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...