how to get date js code example
Example 1: get date javascript
var d= new Date();
d.getFullYear();
d.getMonth();
d.getDate();
d.getHours();
d.getMinutes();
d.getSeconds();
d.getMilliseconds();
d.getTime();
Example 2: js date methods
var d= new Date();
d.getFullYear();
d.getMonth();
d.getDate();
d.getHours();
d.getMinutes();
d.getSeconds();
d.getMilliseconds()
d.getTime();
d.getDay();
d.Date.now();
d.setDate()
d.setFullYear()
d.setHours()
d.setMilliseconds()
d.setMinutes()
d.setMonth()
d.setSeconds()
d.setTime()
Example 3: get date javascript
getDate() in javascript only returns the day as number (1-31)
Example
var d = new Date();
document.getElementById("demo").innerHTML = d.getDate();
Output:
22
Method Description
getFullYear() Get the year as a four digit number (yyyy)
getMonth() Get the month as a number (0-11)
getDate() Get the day as a number (1-31)
getHours() Get the hour (0-23)
getMinutes() Get the minute (0-59)
getSeconds() Get the second (0-59)
getMilliseconds() Get the millisecond (0-999)
getTime() Get the time (milliseconds since January 1, 1970)
getDay() Get the weekday as a number (0-6)
Date.now() Get the time. ECMAScript 5.
Example 4: current date in javascript
const localDate = new Date();
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const days = [
"Sunday",
"Monday ",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const currentDay = days[localDate.getDay()];
const currentDate = localDate.getDate();
const currentMonth = months[localDate.getMonth()];
const currentYear = localDate.getFullYear();
console.log(`${currentDay}, ${currentDate} ${currentMonth}, ${currentYear}`);
Example 5: javascript getdate
var d=new Date();
d.getDate();