<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>Getting the Current Date and Time</title> <script> window.onload = function () { var d = new Date(); var out = ""; out += "Day of Month : " + d.getDate(); out += "<br/>Year : " + d.getFullYear(); out += "<br/>Time : " + d.getHours() + ":" + d.getMinutes(); document.getElementById('result').innerHTML = out; } </script> </head> <body> <div id="result"></div> </body> </html>
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>Setting a Custom Date and Time</title> <script> window.onload = function () { var result = document.getElementById('result'); // args : year, month, day, hours, minutes var d = new Date(2017, 1, 5, 12, 0); result.innerHTML = d; d = new Date(2020, 1, 5, 12, 0); result.innerHTML += "<br/>" + d; result.innerHTML += "<br/>" + d.getDate(); result.innerHTML += "<br/>" + Date.now(); } </script> </head> <body> <div id="result"></div> </body> </html>
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>More Date and Time Functions</title> <script> window.onload = function () { var result = document.getElementById('result'); var d = new Date(); result.innerHTML = d; d.setFullYear(2020); result.innerHTML += "<br/>" + d; d.setHours(20); result.innerHTML += "<br/>" + d; result.innerHTML += "<br/>" + d.toJSON(); } </script> </head> <body> <div id="result"></div> </body> </html>