JS gets a simple instance of the current date and time

  • 2020-03-29 23:50:05
  • OfStack


<script>
var myDate = new Date();
    myDate.getYear();       //Get current year (2 bits)
//alert(myDate.getYear()) //2009
    myDate.getFullYear();   //Gets the full year (4 bits,1970-??).
//alert(myDate.getFullYear()); 2009
    myDate.getMonth();      //Get the current month (0-11,0 for January)
//alert(myDate.getMonth()); //7( The actual is 8 month )
    myDate.getDate();       //Get current day (1-31)
//alert( myDate.getDate()); //13
    myDate.getDay();        //Get the current week X(0-6,0 for Sunday)
//alert(myDate.getDay()); //4  Representative Thursday 
    myDate.getTime();       //Gets the current time (number of milliseconds since 1970.1.1)
myDate.getHours();      //Gets the current number of hours (0-23)
//alert(myDate.getHours()); //9  On behalf of 9 point 
    myDate.getMinutes();    //Gets the current number of minutes (0-59)
//alert(myDate.getMinutes()); //45  points 
    myDate.getSeconds();    //Gets the current number of seconds (0-59)
//alert(myDate.getSeconds()); //40  Number of seconds 
    myDate.getMilliseconds();   //Gets the current number of milliseconds (0-999)
    myDate.toLocaleDateString();    //Get the current date
    var mytime=myDate.toLocaleTimeString();    //Get the current time
//alert(mytime);//9:40:18
    myDate.toLocaleString( );       //Gets the date and time
//alert( myDate.toLocaleString( )); //2009 years 8 month 13 day  9:40:58
</script>

If you want to get a double month and day, you should make your own judgment. For example:

<script>
function curDateTime() {
   var d = new Date();
   var year = d.getYear();
   var month = d.getMonth() + 1;
   var date = d.getDate();
   var day = d.getDay();
   var curDateTime = year;
   if (month > 9)
    curDateTime = curDateTime + month;
   else
    curDateTime = curDateTime + "0" + month;
   if (date > 9)
    curDateTime = curDateTime + date;
   else
    curDateTime = curDateTime + "0" + date;

   //myform.kprq.value = curDateTime;
   alert(" The current date "+curDateTime);
   document.getElementByIdx_x("date").value=curDateTime;
}
</script>


Related articles: