How to Get Day, Month and Year from a Date Object in JavaScript
Topic: JavaScript / jQueryPrev|Next
Answer: 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:
Example
Try this code »<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:
Example
Try this code »<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>
Please check out the tutorial on JavaScript Date and Time to learn more about date and time.
Related FAQ
Here are some more FAQ related to this topic: