Q:

How to Get Day, Month and Year from a Date Object in JavaScript

0

How to Get Day, Month and Year from a Date Object in JavaScript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

<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>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Remove a Specific Item from an Array in Jav... >>
<< How to Check If a Value Exists in an Array in Java...