Javascript Date object

Source: Internet
Author: User
The Date object is a Date and time object. Its main function is to process the Date and time 1. Create the Date object varmyDatenewDate (); or varmyDatenewDate (& quot; July21, 198301: 15: 00 & quot;); // This method is used to customize the date and time format... syntax Date is a Date and time object. Its main function is to process the Date and time.
1. Create a Date object
Var myDate = new Date ();
Or
Var myDate = new Date ("July 21,198 3 01:15:00"); // This method is used to customize the Date and time. If the format is incorrect, an Invalid Date prompt is displayed.
The constructor attribute of the Date object is: Date
Document. write (myDate. constructor = Date); // Output true
2. The Date () method returns the Date and time of today. The format is fixed:
Document. write (Date (); // Date () is a built-in Javascript Object and can be directly used.
Output:
Fri Oct 26 2012 10:15:22 GMT + 0800
Format: Week month date year time Time Zone
In addition, if you define a Date () object, the returned results are the same.
Var myDate = new Date ();
Document. write (myDate );
Fri Oct 26 2012 10:17:09 GMT + 0800
Note: The difference between the two lies in:
The former can only display the current time, but cannot define the time at will.
For example, document. write (Date ("July 21,198 3 01:15:00 "));
The displayed time is still the current time: Fri Oct 26 2012 10:15:22 GMT + 0800
3. The getDate () method returns a day of the month.
A day in a month refers to the use of local time, and the return value is 1 ~ An integer between 31.
Var myDate = new Date ();
Document. write (myDate. getDate ());
Output: 26
Var myDate = new Date ("July 21,198 3 01:15:00 ");
Document. write (myDate. getDate ());
Output: 21
4. The getDay () method returns a number representing a day of the week. The value range is 0--6.
Var myDate = new Date ("July 21,198 3 01:15:00 ");
Document. write (mtDate. getDay ());
Output 4
For this reason, we can use the Date object and Array object to display the time in a user-friendly manner. The following method is very commonly used.
Var weekday = new Array (7 );
Weekday [0] = "Sunday ";
Weekday [1] = "Monday ";
Weekday [2] = "Tuesday ";
Weekday [3] = "Wednesday ";
Weekday [4] = "Thursday ";
Weekday [5] = "Friday ";
Weekday [6] = "Saturday ";
Var myDate = new Date ();
Document. write (weekday [myDate. getDay ()]);
5. getMonth () returns the month field. The returned value is an integer between 0 (January) and 11 (December ).
Similar to getDay (), we also use a combination of Array objects.
Var d = new Date ();
Var month = new Array (12 );
Month [0] = "January ";
Month [1] = "February ";
Month [2] = "March ";
Month [3] = "April ";
Month [4] = "May ";
Month [5] = "June ";
Month [6] = "July ";
Month [7] = "August ";
Month [8] = "September ";
Month [9] = "October ";
Month [10] = "November ";
Month [11] = "December ";
Document. write ("The month is" + month [d. getMonth ()]);
6. The getFullYear () method returns a four-digit number indicating the year.
The returned value is a four-digit number, indicating a complete year including the century value, rather than a two-digit abbreviation.
Var d = new Date ();
Document. write (d. getFullYear ());
Output:
2012
Var born = new Date ("July 21,198 3 01:15:00 ");
Document. write ("I was born in" + born. getFullYear ());
Output:
1983
By combining the above three methods, we can get a more user-friendly time display, for example:
Var weekday = new Array (7 );
Weekday [0] = "Sunday"
Weekday [1] = "Monday"
Weekday [2] = "Tuesday"
Weekday [3] = "Wednesday"
Weekday [4] = "Thursday"
Weekday [5] = "Friday"
Weekday [6] = "Saturday"
Var month = new Array (12 );
Month [0] = "January ";
Month [1] = "February ";
Month [2] = "March ";
Month [3] = "April ";
Month [4] = "May ";
Month [5] = "June ";
Month [6] = "July ";
Month [7] = "August ";
Month [8] = "September ";
Month [9] = "October ";
Month [10] = "November ";
Month [11] = "December ";
Var myDate = new Date ();
Document. write ("Date is" + myDate. getFullYear () + "" + month [myDate. getMonth ()] + "" + myDate. getDate () + "" + weekday [myDate. getDay ()]);
Output:
Date is 2012 October 19 Friday
7. The getHours () method returns the hour field of the time. The returned value is an integer between 0 (midnight) and 23.
Var born = new Date ("July 21,198 3 01:15:00 ");
Document. write (born. getHours ());
Output: 1
Note: The value returned by getHours () is a two-digit number.
However, the return value is not always two digits. If the value is smaller than 10, only one digit is returned.
8. The getMinutes () method returns the minute field of the time. The returned value is 0 ~ An integer between 59.
Similar to the preceding method, the return value is not always two digits. If the value is smaller than 10, only one digit is returned.
9. The getSeconds () method returns the second of the time, and the return value is 0 ~ An integer between 59.
Similar to the preceding method, the return value is not always two digits. If the value is smaller than 10, only one digit is returned.
10. The getMilliseconds () method returns the millisecond and millisecond fields in the local time. The returned value is 0 ~ An integer between 999.
Note: The value returned by getMilliseconds () is a three-digit number.
However, the return value is not always three digits. If the value is less than 100, only two digits are returned. If the value is less than 10, only one digit is returned.
There are two ways to display the time:
---> Method 1
Var d = new Date ();
Document. write ('Time is '+ d. getHours () + ":" + d. getMinutes () + ":" + d. getSeconds ());
Output: Time is. The disadvantage is obvious. Only one digit is displayed when the second is <10. Others are similar.
Therefore, the second display mode is recommended.
---> Method 2
Function checktime (time)
{
If (time <10)
{
Time = '0' + time;
}
Return time;
}
Var d = new Date ();
Document. write ('Time is '+ checktime (d. getHours () + ":" + checktime (d. getMinutes () + ":" + checktime (d. getSeconds ()));
Output: Time is 10:55:02
11. The getTime () method returns the number of milliseconds between the local time and January 1, January 1, 1970. Therefore, you can customize the Date ("Jul 26 2000 ");
Var d = new Date ();
Document. write (d. getTime () + "milliseconds since 1970/01/01 ");
Output: 1350615452813 milliseconds since 1970/01/01
Therefore, we can obtain the number of years from the local time.
Var minutes = 1000*60; // 60 seconds per minute, 60 milliseconds per second, and so on
Var hours = minutes * 60;
Var days = hours * 24;
Var years = days * 365;
Var d = new Date ();
Var t = d. getTime (); // obtain the number of milliseconds from
Var y = t/years;
Document. write ("It's been:" + y + "years since 1970/01/01! ");
Output:
It's been: 42.82773990521943 years since 1970/01/01!
Note: At this time, the year is a decimal number and can be changed to an integer (in fact, there is no difference between integers in Javascript)
The last line is changed:
Document. write ("It's been:" + parseInt (y) + "years since 1970/01/01! ");
Output:
It's been: 42 years since 1970/01/01!
ParseInt (float); the floating point type can be converted to an integer.
12. The getTimezoneOffset () method returns the time difference between Greenwich Mean Time and local time, in minutes.
Note:
The getTimezoneOffset () method returns the number of minutes that differ between the local time and the GMT time or UTC time.
In fact, this function tells us the time zone for running JavaScript code and whether the specified time is runtime.
The returned result is measured in minutes rather than hours, because the time zone occupied by some countries is less than an hour.
Var d = new Date ();
Document. write (d. getTimezoneOffset ());
Output:
-480 (UTC + 8, 8*60)
Therefore, you can use this method to determine the time zone
Function checkzone (zone)
{
If (zone = 0)
{
Return "Zero Time Zone ";
}
Else if (zone> 0)
{
Return "West" + parseInt (zone/60) + "zone ";
}
Else
{
Return "East" + parseInt (Math. abs (zone/60) + "zone ";
}
}
Var d = new Date ();
Document. write (checkzone (d. getTimezoneOffset ()));
Math. abs () is the absolute value.
The above method is original, because the geography is not good, do not know right, please correct
13. The parse () method parses a date and time string and returns the number of milliseconds between midnight and the date and time.
Parse (str); str is a string in time format.
Var minutes = 1000*60;
Var hours = minutes * 60;
Var days = hours * 24;
Var years = days * 365;
Var t = Date. parse ("Jul 8, 2005 ");
Var y = t/years;
Document. write ("It's been:" + parseInt (y) + "years from 1970/01/01 ");
Document. write ("to 2005/07/08! ");
Output:
It's been: 35 years from 1970/01/01 to 2005/07/08!
14. The setDate () method is used to set a day of a month.
SetDate (day) is required. Represents a value (1 ~ 31 ).
Note: This method is modified based on the original object and changes the value of the original data.
Example:
Var d = new Date ();
Document. write (d. getDate () +"
");
D. setDate (31 );
Document. write (d. getDate ());
Output:
19
31
15. The setMonth () method is used to set the month. It is used with setDate () and changes the original Date object.
SetMonth (month, day). The second parameter may not be supported currently, and month is required. A value indicating the month. The value ranges from 0 to January )~ Between 11 (December.
If the "day" parameter is supported, "day" indicates the value of a day in the month, ranging from 1 ~ Between 31. It is represented by local time.
Var d = new Date ();
Document. write (d. getMonth () + "" + d. getDate () +"
");
D. setMonth (11,26 );
Document. write (d. getMonth () + "" + d. getDate ());
Output:
9 19
11 26
16. setFullYear () is used to set the year.
SetFullYear (year, month, day );
Year is required. The four-digit integer of the year. It is represented by local time.
Month is optional. Indicates the value of the month, ranging from 0 ~ Between 11. It is represented by local time.
Day is optional. Indicates the value of a day of the month, ranging from 1 ~ Between 31. It is represented by local time.
Var d = new Date ();
D. setFullYear (1992,10, 3 );
Document. write (d );
Output:
Tue Nov 03 1992 11:31:58 GMT + 0800
17. The setHours () method is used to set the hour field of the specified time.
SetHours (hour, min, sec, millisec );
Hour is required. The value of the hour. It is between 0 (midnight) and )~ Between 23 (PM), in the local time (the same below ).
Min is optional. The minute value, ranging from 0 ~ Between 59. This parameter is not supported before EMCAScript standardization.
Optional for sec. The second value, ranging from 0 ~ Between 59. This parameter is not supported before EMCAScript standardization.
Millisec is optional. It is a millisecond value ranging from 0 ~ In the range of 999. This parameter is not supported before EMCAScript standardization.
Var d = new Date ()
D. setHours (15, 35, 1)
Document. write (d)
Output:
Fri Oct 19 15:35:01 Coordinated Time + 0800 2012
18. The setMinutes () method is used to set the minute field of the specified time.
SetMinutes (min, sec, millisec)
Min is required. The minute value, ranging from 0 ~ Between 59, in the local time (the same below ).
Optional for sec. The second value, ranging from 0 ~ Between 59. This parameter is not supported before EMCAScript standardization.
Millisec is optional. It is a millisecond value ranging from 0 ~ In the range of 999. This parameter is not supported before EMCAScript standardization.
Var d = new Date ()
D. setMinutes (1)
Document. write (d)
Output:
Fri Oct 19 11:01:11 Coordinated Time + 0800 2012
19. The setSeconds () method is used to set the second field of the specified time.
SetSeconds (sec, millisec)
The sec is required. The number of seconds. The value ranges from 0 ~ An integer between 59.
Millisec is optional. It is a millisecond value ranging from 0 ~ In the range of 999. This parameter is not supported before EMCAScript standardization.
20. The setMilliseconds () method is used to set the millisecond field of the specified time.
SetMilliseconds (millisec)
Millisec required. This parameter is used to set the dateObject millisecond field, which is between 0 and ~ An integer between 999.
21. The setTime () method sets the Date object in milliseconds.
This method is a common method for storing Date. getTime () in the database in milliseconds,
This method is used to display the returned results.
SetTime (millisec)
Millisec required. The number of milliseconds between the date and time to be set according to the GMT time between midnight, January 1, January 1, 1997.
This type of millisecond value can be passed to the Date () constructor, which can be obtained by calling the Date. UTC () and Date. parse () methods. Represents a date in milliseconds to make it independent of the time zone.
Var d = new Date ();
D. setTime (77771564221 );
Document. write (d );
Output:
Mon Jun 19 1972 11:12:44 GMT + 0800
This method converts the long data type stored in the database to a time object.
22. The toTimeString () method converts the time part of the Date object to a string and returns the result.
Var d = new Date ();
Document. write (d. toTimeString ());
Output:
11:50:57 GMT + 0800
23. The toDateString () method converts the Date part of the Date object to a string and returns the result.
Var d = new Date ();
Document. write (d. toDateString ());
Output:
Fri Oct 19, 2012
24. There are also many methods for designing the UTC time zone, which are not listed here. They are just a summary of common and more common methods.
 

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.