//specify time minus 2 secondsfunctionReducetwos (DATESTR) {//datestr format is yyyy-mm-dd hh:mm:ssvardt=NewDate (Datestr.replace (/-/, "/"));//converts an incoming date format string to a Date object compatible with IE //var dt=new date (DATESTR);//Convert the passed-in date format string to a Date object non IEvarndt=NewDate (Dt.gettime ()-2000);//subtract the converted time by two secondsvarresult={year:parseint (Ndt.getfullyear ()), Month:parseint (Ndt.getmonth ()+1), Day:parseint (Ndt.getday ()), Hour:parseint (Ndt.gethours ()), Minute:parseint (Ndt.getminutes ()), Secon D:parseint (Ndt.getseconds ())}returnresult;}
The first acquired day was not correct and improved a bit
#求时间差改了, instead of half an hour ago
functionShowtime () {varDatestr= ' 2017-07-18 13:21:06 ';vardt=NewDate (Datestr.replace (/-/, "/"));varDate=NewDate (Dt.gettime ()-(30*60*1000));varSeperator1 = "-"; varSeperator2 = ":"; varmonth = Date.getmonth () + 1; varStrdate =date.getdate (); if(Month >= 1 && month <= 9) {Month= "0" +month; } if(strdate >= 0 && strdate <= 9) {strdate= "0" +strdate; } varcurrentdate = date.getfullyear () + seperator1 + month + Seperator1 +strdate+ "" + date.gethours () + Seperator2 +date.getminutes ()+ Seperator2 +date.getseconds (); returncurrentdate;}
The idea is: Change Yyyy-mm-dd hh:mm:ss (JS hh to 24-hour system) to seconds, and then subtract two values.
Then convert the value to a Date object
Execution Result:
Have a little friend to know, to help answer, why this day has changed (for fancy amputated)
JS gets the first few seconds of the specified time