How to convert a string to a time in JS and calculate the difference

Source: Internet
Author: User
Tags difference between two times

In the foreground page development usually use to calculate two time difference, first attach the implementation method

[JavaScript]View PlainCopy 
  1. End time
  2. End_str = ("2014-01-01 10:15:00"). Replace (/-/g,"/");   Generally get the format of the time is: Yyyy-mm-dd hh24:mi:ss, so I use this as an example, is the format, you do not have to replace.
  3. var end_date = new Date (END_STR); Convert a string to a time
  4. Start time
  5. Sta_str = ("2014-01-01 10:15:00"). Replace (/-/g,"/");
  6. var sta_date = new Date (STA_STR);
  7. var num = (end_date-sta_date)/(1000*3600*24); //Find out the time difference between two times, this is the number of days
  8. var days = parseint (Math.ceil (num)); //Convert to full day (less than 0 of the play does not have to turn)
  9. The following is attached to the JS in some of the methods for Time class operation
  10. var mydate = new Date ();
  11. Mydate.getyear (); //Get Current year (2-bit)
  12. Mydate.getfullyear ();    //Get the full year (4-bit, 1970-????)
  13. Mydate.getmonth (); //Get the current month (0-11, 0 for January)
  14. Mydate.getdate (); //Get current day (1-31)
  15. Mydate.getday (); //Get Current week x (0-6, 0 for Sunday)
  16. Mydate.gettime (); //Gets the current time (number of milliseconds since 1970.1.1)
  17. Mydate.gethours (); //Get current hours (0-23)
  18. Mydate.getminutes (); //Gets the current number of minutes (0-59)
  19. Mydate.getseconds (); //Gets the current number of seconds (0-59)
  20. Mydate.getmilliseconds (); //Gets the current number of milliseconds (0-999)
  21. Mydate.tolocaledatestring (); //Get the current date
  22. var mytime=mydate.tolocaletimestring (); //Get current time
  23. Mydate.tolocalestring ();   //Get date and time----if it involves a minute or seconds, use it directly.

How to convert a string to a time in JS and calculate the difference

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.