JavaScript Date Object

Source: Internet
Author: User

Original: JavaScript Date object

Using JavaScript for so long, has not been well organized, now start to tidy up the knowledge, consolidate.

Start with the Date object first.

The Date object is used for processing time, and since it is an object, we can use the New keyword to get the object, and then we can use the method provided by the date object itself. You can use the following method to get a Date object

1,var objdate=new Date (),//date object automatically uses the current date and time as its initial value.

2, var objdate=new Date (Dateval); Converts the given number of milliseconds to the time used

3, var objdate=new date (year monthdate[hours[minutes[seconds[,ms]]]]) );//Specify a specific date

The more common is the first way and the third way.

Let's take a look at the parameters used above

Dateval

Required option. If it is a numeric value,Dateval represents the number of milliseconds between the specified date and the global standard Time between midnight January 1, 1970. If it is a string, Dateval is parsed according to the rules in the parse method. The dateval parameter can also be the Vt_date value returned from some ActiveX (R) objects.

Year

Required option. The full year, for example, 1976 (instead of 76).

Month

Required option. Represents the month, which is an integer from 0 to 11 (January to December).

Date

Required option. Represents a date, which is an integer from 1 to 31.

Hours

Options are available. If minutes is provided, it must be given. Represents the hour, which is an integer from 0 to 23 (midnight to 11pm).

Minutes

Options are available. If seconds is provided, it must be given. Represents the minute, which is an integer from 0 to 59.

Seconds

Options are available. If milliseconds is provided, it must be given. Represents the second, which is an integer from 0 to 59.

Ms

Options are available. Represents milliseconds, which is an integer from 0 to 999.

Having acquired the object of time, we can use this object, for example

var date=new date ();d ocument.write (date);

The above code get is the time of the machine, the reality of time for Mon Mar 15:43:04 gmt+0800 (China Standard Time), it is obvious that this display format is not what we want, so we need to use the Date object method, To make your own time display format.

However, date this object provides many methods, commonly used to get the time method also getyear (), getFullYear (), GetMonth (), GetDate (), GetDay (), getHours (), getminutes (), Getseconds (), GetTime () and so on, below is a brief introduction to these methods

GetYear () returns the local year in the Date object, which is obsolete and provides this method for backward compatibility. Use the getfullyear method instead. For 1900-1999, the returned year value is a two-digit integer that represents the gap between the saved year and the 1900-year period. For other years, the return value is a four-bit integer. For example, the return value for 1996 is 96, and the return values for 1825 and 2025 are correspondingly 1825 and 2025.

note for JScript version 1.0, the value returned byGetYear is always the gap between the year in the Date object and the 1900 year. For example, the return value for 1899 is-1, and 2000 's return value is 100.

var date = new Date ();d Ocument.write (Date.getyear ());

The result is 115, not the 2015 we expect.

getFullYear () to get the year value in global Standard Time (UTC), use the getutcfullyear method. The getFullYear method returns the year value in the form of an absolute number. For example, the return value for 1976 is 1976. This avoids the problem of 2000 and does not confuse dates after January 1, 2000 with dates after January 1, 1900.

var date = new Date ();d Ocument.write (Date.getfullyear ());

The result is the 2015 we're looking for.

GetMonth () Gets the month value in the local timesheet, the GetMonth method returns an integer from 0 to 11 that represents the month value in the Date object. This integer is not equal to the Convention to represent the number of months, but rather 1 less than the value represented by convention. If the time value saved in a date object is "Jan 5, 2015 08:47:00", then the GetMonth () method returns 0.

            var date = new Date ();            document.write (Date.getmonth () + "</br>");            document.write (Date.getutcmonth () + "</br>");            var setDate = new Date ("Jan 5, 08:47:00");            document.write (Setdate.getmonth () + "</br>");

The above example shows the result is 2 2 0, so to display the correct month to the corresponding add 1

GetDate () Gets the number of months in the current time and the return value is an integer between 1 and 31 that represents the date value in the corresponding Date object.

var date = new Date ();d Ocument.write (date.getdate () + "</br>");

The result of the operation is 9

GetDay () represents the number of weeks in the current time date, and the value returned by the GetDay method is an integer between 0 and 6 that represents the day of the week

GetDay () return value The corresponding week value
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday

The GetHours () method returns an integer between 0 and 23 that represents the number of hours that are calculated from midnight. In the following two cases, the return value of this method is 0: The time is before 1:00:00 am, or the time is not saved in the object when the date object is created. The only way to determine what is the case is to further check if the minute and second values are also 0. If both values are 0, it is almost certain that the time value is not saved to the date object.

The Getminutes () method returns an integer from 0 to 59, which equals the minute value that is held in the Date object. The return value is 0 in the following two cases: it takes less than a minute after the hour of the clock, or a time value is not saved to the object when the date object is created. The only way to determine what is the case is to check whether the hour and second values are also 0. If both values are 0, it is almost certain that you have not saved the time value to the date object.

The Getseconds () method returns an integer between 0 and 59 that represents the second value in the corresponding Date object. In the following two cases, the return value is 0. The first case is that the time elapsed in the current minute is less than one second. Another scenario is when you create a date object without saving the time value to the object. The only way to determine what is the case is to check whether the hour and minute values are also 0. If both values are 0, it is almost certain that the time value is not saved to the date object.

The GetTime () method returns an integer value that represents the number of milliseconds between the time that is calculated from January 1, 1970 to the date object. The range of dates is approximately 285,616 years before and after midnight on January 1, 1970. Negative numbers represent dates prior to 1970.

As for setting the time, we can use the various set methods provided by the Date object, but the simplest is that we can initialize the time in the new object, for example:

           var date = new Date ("");            document.write (Date.getfullyear () + "</br>");            var setDate = new Date ();            Setdate.setfullyear ();            document.write (Setdate.getfullyear () + "</br>");

The results are all the same, all 2016, but the data is written differently, and the new Date () is filled with the string setfullyear () is the corresponding integer.

Here's a clock that shows the current date in a combined array

function Shownowdate ()        {            var date = new Date ();            var year = Date.getfullyear ();            var month = Date.getmonth ();            var day = Date.getdate ();            var ismonth = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");            var isday = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");            document.write ("Today's date is:" +year+ "Year" +ismonth[month]+day+ "Day" + "" +isday[date.getday ()]);        }

Displays the clock at the current specific time

function Showtimemethod () {var date = new Date (), var hour = date.gethours (); var minuts = Date.getminutes (); var seconds = da Te.getseconds ();d Ocument.getelementbyid ("ShowTime"). Value = Hour + ":" + minuts + ":" + seconds;settimeout (" Showtimemethod () ", 100);}

JavaScript Date Object

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.