JavaScript calculates the number of days in a two-day period

Source: Internet
Author: User

Sometimes we need to calculate the number of days or hours in two days. Next we will use JavaScript to achieve this requirement, and then we will learn some JavaScript functions that need to be used.

The JavaScript program is as follows:

<Script type = "text/javascript"> var getOffDays = function (startDate, endDate) {// get the difference var mmSec = (endDate. getTime ()-startDate. getTime (); // The unit is converted to a day and return (mmSec/3600000/24);}; alert (getOffDays (new Date (2009,8, 7), new Date (2010,9, 17); </script>
JavaScript getTime () method

The getTime () method returns the total number of milliseconds that have been accumulated since January 1, 1970. Use dateObject. getTime (). This method must be used in combination with the Date object.

The following program obtains the total number of milliseconds accumulated since January 1, 1970 and outputs it:

    <script type="text/javascript">var d = new Date()document.write(d.getTime() + " milliseconds since 1970/01/01")</script>

The program running result is:

1284655348088 milliseconds since 1970/01/01

The following program gets the total number of years used since January 1, 1970 and outputs it:

<script type="text/javascript">var minutes = 1000*60var hours = minutes*60var days = hours*24var years = days*365var d = new Date()var t = d.getTime()var y = t/yearsdocument.write("It's been: " + y + " years since 1970/01/01!")</script>

The program running result is:

It's been: 40.73615794193937 years since 1970/01/01!
JavaScript Date object

The Date object is used to process the Date and time. You can use the new keyword to define the Date object. The following code defines a Date object named myDate:

var myDate = new Date();

The Date object automatically uses the current Date and time as its initial value.

By using the date object method, we can easily operate on the date. In the following example, we set a specific date for the date object (January 1, August 9, 2008 ):

var myDate = new Date();myDate.setFullYear(2008,7,9);

Indicates that the month parameter is between 0 and 11. That is to say, if you want to set the month to August, the parameter should be 7.

In the following example, we set the date object to the date after 5 days:

var myDate = new Date();myDate.setDate(myDate.getDate()+5);

If the number of days increases, the month or year is changed.

A date object can also be used to compare two dates. The following code compares the current date with August 9, 2008:

var myDate=new Date();myDate.setFullYear(2008,7,9);var today = new Date();if (myDate>today){alert("Today is before 9th August 2008");}else{alert("Today is after 9th August 2008");}

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.