xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to get year, month, date and time separately in JavaScript
JavaScript Online Editor
Run Code
More JavaScript Sample Codes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>How to get year, month, date and time seperately in JavaScript</title> <!--Script tag is added to write JavaScript code inside it--> <script> // Months are 0-indexed, so 10 represents November var currentDateTime = new Date(); //get the current date and time var year = currentDateTime.getFullYear(); var month = currentDateTime.getMonth(); //month are 0 based, so 0 is January var date = currentDateTime.getDate(); var hour = currentDateTime.getHours(); var minute = currentDateTime.getMinutes(); var second = currentDateTime.getSeconds(); function myFunction() { document.getElementById("outPut").innerHTML = "Your current datetime parts: </br>" +"Year: " + year + "</br>" +"Month: " + month +"</br>" +"Day: " + date +"</br>" +"Hour: " + hour +"</br>" +"Minute: " + minute +"</br>" +"Second: " + second +"</br>"; }; </script> </head> <body> <h1>How to Get Year, Month, Date and Time Seperately in JavaScript?</h1> <button onclick="myFunction()">Click here to see result</button> <p id="outPut"></p> </body> </html>