One, to meet the reading habits of the date format, directly displayed on the Web page, without the form of a popup box
Ideas:
1. Get the current date time object
2. The date and time of the year and seconds, respectively, to get out, stitching into a string of style
functionGetdatedemo () {varDate =NewDate (); varYear =date.getfullyear (); varmonth = Date.getmonth () +1; varDay =date.getdate (); if(month<10) Month= "0" +month; if(day<10) Day= "0" + DayvarHours =date.gethours ()varminutes =date.getminutes (); varSecond =date.getseconds (); //stitched into March 26, 2014 20:23:23 varDateTime = year+ "year" +month+ "month" +day+ "Day" + "+hours+": "+minutes+": "+second; document.write (DateTime); } getdatedemo ();
Extension: Automatically increases the time on a webpage (that is, displays a clock), using the SetInterval (code, time interval) method in the window, noting that the code is wrapped in double quotes
<script type= "Text/javascript" >functionGetdatedemo () {varDate =NewDate (); varYear =date.getfullyear (); varmonth = Date.getmonth () +1; varDay =date.getdate (); if(month<10) Month= "0" +month; if(day<10) Day= "0" +Day ; varHours =date.gethours (); if(hours<10) Hours= "0" +hours; varminutes =date.getminutes (); if(minutes<10) Minutes= "0" +minutes; varSecond =date.getseconds (); if(second<10) Second= "0" +second; varMsecond =date.getmilliseconds (); //stitched into March 26, 2014 20:23:23 varDateTime = year+ "year" +month+ "month" +day+ "Day" + "+hours+" when "+minutes+" minutes "+second+" seconds +Msecond; //document.write (dateTime); //Get node method through DOM, find DIV node tag document.getElementById (); varDivnode = document.getElementById ("Time"); //using the div properties: InnerHTMLdivnode.innerhtml =DateTime; } window.setinterval ("Getdatedemo ()", 100);</script><style type= "Text/css" >div{width:242px; float : Left; }</style>Two: Calculation of the date of the object: calculates the number of days between two hours: 2013-2-3 ———————— 2014-3-4
Ideas:
Converting a string to a Date object (millisecond value) uses the parse () method of the Date object, subtracting two millisecond values until the millisecond value/1000/60/60/24
Attention:
The parse () method passes in a format that is valid for months/days/years, etc.
The result of subtracting two millisecond values may be negative, but the time is not negative, so the result should be obtained when the value of the millisecond is converted in absolute/1000/60/60/24
function Datedemo () { var day1 = ' 2/3/2013 '; var day2 = ' 22/6/2014 '; var time1 = Date.parse (day1); // the millisecond value of the return time var time2 = date.parse (day2); Alert ((Math.Abs (time2-time1)/1000/60/60/24); } Datedemo ();
JavaScript's Date Object practice