Use the Date()
Object Methods
You can use the Date object methods getDate()
, getMonth()
, and getFullYear()
to get the date, month and full-year from a Date object. Let's check out an example:
<script>
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1; // Since getMonth() returns month from 0-11 not 1-12
var year = d.getFullYear();
var dateStr = date + "/" + month + "/" + year;
document.write(dateStr);
</script>
The getDate()
, getMonth()
, and getFullYear()
methods returns the date and time in the local timezone of the computer that the script is running on. If you want to get the date and time in the universal timezone just replace these methods with the getUTCDate()
, getUTCMonth()
, and getUTCFullYear()
respectivley, as shown in the following example:
<script>
var d = new Date();
var date = d.getUTCDate();
var month = d.getUTCMonth() + 1; // Since getUTCMonth() returns month from 0-11 not 1-12
var year = d.getUTCFullYear();
var dateStr = date + "/" + month + "/" + year;
document.write(dateStr);
</script>