The Date object and the GetTime () method in JavaScript

Source: Internet
Author: User

Sometimes we need to calculate the days of two days, or the number of hours, and so on. Use JavaScript to implement this requirement, and then learn some of the JavaScript functions you need to use. Chizhou-Sheng Crafts

The JavaScript program is as follows:

1 <script type="text/javascript"
2 vargetOffDays = function(startDate, endDate) {  
3     //得到时间戳相减 得到以毫秒为单位的差  
4     varmmSec = (endDate.getTime() - startDate.getTime());
5     //单位转换为天并返回 
6     return(mmSec / 3600000 / 24); 
7 };  
8 alert(getOffDays(newDate(2009,8,7), newDate(2010,9,17)));  
9 </script>
JavaScript GetTime () method

The GetTime () method returns the total number of milliseconds accumulated since January 1, 1970. Use Dateobject.gettime (), which is used in conjunction with the Date object.

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

1 <script type="text/javascript">
2 vard = newDate()
3 document.write(d.getTime() + " milliseconds since 1970/01/01")
4 </script>

The result of the program operation is:

1 1284655348088 milliseconds since 1970/01/01

The following program we get the total number of years that have been spent since January 1, 1970 and output it:

01 <script type="text/javascript">
02 varminutes = 1000*60
03 varhours = minutes*60
04 vardays = hours*24
05 varyears = days*365
06 vard = newDate()
07 vart = d.getTime()
08 vary = t/years
09 document.write("It‘s been: "+ y + " years since 1970/01/01!")
10 </script>

The result of the program operation is:

1 It‘s been: 40.73615794193937 years since 1970/01/01!
JavaScript Date (date) object

The Date object is used to process dates and times. You can define a Date object by using the New keyword. The following code defines a Date object named MyDate:

1 varmyDate = newDate();

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

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

1 varmyDate = newDate();
2 myDate.setFullYear(2008,7,9);

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

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

1 varmyDate = newDate();
2 myDate.setDate(myDate.getDate()+5);

If you increase the number of days to change the month or year, the Date object will automatically complete the conversion.

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

View Source print?
01 varmyDate=newDate();
02 myDate.setFullYear(2008,7,9);
03 vartoday = newDate();
04 if(myDate>today)
05 {
06     alert("Today is before 9th August 2008");
07 }
08 else
09 {
10     alert("Today is after 9th August 2008");
11 }

Date objects and gettime () methods in JavaScript

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.