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

1704 views 2 years ago Javascript

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>

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]