Kyler Phillips Kyler Phillips - 4 months ago 17
Javascript Question

How to get current date in ddth/mm/yyyy format using JQuery

How can I get the current date in JQuery as a variable and then append to a heading in with an id of

currentdate
, with the format
24th July 2016
?

Answer

You need to use javascript Date() object and method of it to getting current date and component of it.

var monthName = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date();
var dateStr = date.getDate() + "th " + monthName[date.getMonth()] + " " + date.getFullYear();

$("#currentdate").text(dateStr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="currentdate"></div>