JS gets the first few seconds of the specified time

Source: Internet
Author: User


//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.