...
Code Block |
---|
var d = new Date("2020-07-30") // note you should type Date not date !!! |
If you want to get its element value like FullYear, Month, Date, Hours, and etc. You can simply get it like below.
Code Block |
---|
var d = new Date();
document.getElementById("demo").innerHTML += d.getFullYear(); // Get the year as a four digit number (yyyy)
document.getElementById("demo").innerHTML += d.getMonth(); // Get the month as a number (0-11)
document.getElementById("demo").innerHTML += d.getDate(); // Get the day as a number (1-31)
document.getElementById("demo").innerHTML += d.getHours(); // Get the hour (0-23)
document.getElementById("demo").innerHTML += d.getMinutes(); // Get the minute (0-59)
document.getElementById("demo").innerHTML += d.getSeconds(); // Get the second (0-59)
document.getElementById("demo").innerHTML += d.getMilliseconds(); // Get the millisecond (0-999)
document.getElementById("demo").innerHTML += d.getTime(); // Get the time (milliseconds since January 1, 1970)
document.getElementById("demo").innerHTML += d.getDay(); // Get the weekday as a number (0-6)
document.getElementById("demo").innerHTML += Date.now(); // Get the time. ECMAScript 5. |
To convert Date type variable to string in Javascript
Code Block |
---|
var d = new Date();
var n = d.toString(); |
To set values for Date()
Code Block |
---|
var d= new Date("1973-02-16")
d.setFullYear(2020); // Set the year (optionally month and day)
d.setFullYear(2020,12,30); // Set the year (optionally month and day)
d.setMonth(11); // Set the month (0-11)
d.setDate(25); // Set the day as a number (1-31)
d.setHours(12); // Set the hour (0-23)
d.setMilliseconds(22); // Set the milliseconds (0-999)
d.setMinutes(10); // Set the minutes (0-59)
d.setSeconds(0); // Set the seconds (0-59)
d.setTime(12311231); // Set the time (milliseconds since January 1, 1970) |
To increase one day in ISO Date
Code Block |
---|
function IncDate( src) // src should be "YYYY-MM-DD" like "2009-04-13" { var d = new Date(src); d.setDate(d.getDate()+1); var ret = d.getFullYear().toString() + "-" + ((d.getMonth()<9) ? "0":"") + (d.getMonth()+1).toString() + "-" + ((d.getDate()<10) ? "0":"") + d.getDate().toString(); return ret; } |