Home  >  Article  >  Web Front-end  >  A simple example of getting the current date and time with JS_javascript skills

A simple example of getting the current date and time with JS_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:13:51913browse

Copy code The code is as follows:

<script><br>var myDate = new Date ();<br> myDate.getYear(); //Get the current year (2 digits)<br>//alert(myDate.getYear()) //2009<br> myDate.getFullYear(); //Get the complete Year (4 digits, 1970-????)<br>//alert(myDate.getFullYear()); 2009<br> myDate.getMonth(); //Get the current month (0-11, 0 represents 1 Month)<br>//alert(myDate.getMonth()); //7 (actually August)<br> myDate.getDate(); //Get the current day (1-31)<br>//alert ( myDate.getDate()); //13<br> myDate.getDay(); //Get the current week X (0-6, 0 represents Sunday)<br>//alert(myDate.getDay()); / /4 represents Thursday<br> myDate.getTime(); //Get the current time (the number of milliseconds since 1970.1.1)<br>myDate.getHours(); //Get the current hours (0-23)<br>//alert(myDate.getHours()); //9 represents 9 o'clock<br> myDate.getMinutes(); //Get the current minutes (0-59)<br>//alert(myDate.getMinutes() ); //45 minutes<br> myDate.getSeconds(); //Get the current seconds (0-59)<br>//alert(myDate.getSeconds()); //40 seconds<br> myDate. getMilliseconds(); //Get the current milliseconds (0-999)<br> myDate.toLocaleDateString(); //Get the current date<br> var mytime=myDate.toLocaleTimeString(); //Get the current time<br>/ /alert(mytime);//9:40:18<br> myDate.toLocaleString( ); //Get date and time<br>//alert( myDate.toLocaleString( )); //August 13, 2009 9:40:58 <p></script>


If you want to get double months and days, you have to make your own judgment, for example:
Copy code The code is as follows:

<script><br>function curDateTime() {<br> var d = new Date();<br> var year = d.getYear();<br> var month = d.getMonth() 1;<br> var date = d.getDate();<br> var day = d.getDay();<br> var curDateTime = year;<br> if (month > 9)<br> curDateTime = curDateTime month;<br> else<br> curDateTime = curDateTime "0" month;<br> if (date > 9)<br> curDateTime = curDateTime date;<br> else<br> curDateTime = curDateTime "0" date;<br><br> //myform.kprq.value = curDateTime;<br> alert("Current Date" curDateTime);<br> document.getElementByIdx_x("date").value=curDateTime; <p>}<br></script>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn