JavaScript Date Object Detailed _ Basics

Source: Internet
Author: User
Tags abs datetime getdate local time numeric value

Date object is a DateTime object whose primary function is to implement a date-time processing

1. Create Date Object

Copy Code code as follows:
var mydate = new Date ();

Or
Copy Code code as follows:
var mydate = new Date ("July 21, 1983 01:15:00");//This method is the way to customize DateTime, if the format is not correct for prompt invalid date
The constructor property of the Date object is: Date
document.write (Mydate.constructor = = Date);//Output True

2, Date () method, returns today's date and time, in a fixed format:

Copy Code code as follows:
document.write (Date ());//date () a built-in object that belongs to JavaScript and can be used directly with the

Output:
Fri Oct 10:15:22 gmt+0800
Format: Day of the week month date year time zone

Another: If you customize a date () object, the return result is the same

Copy Code code as follows:
var mydate = new Date ();
document.write (mydate);

Fri Oct 10:17:09 gmt+0800

Note: The difference between the two is:
The former can only display the current time, but cannot define the time arbitrarily
For example, document.write (Date ("July 21, 1983 01:15:00"));
The time it shows is still the current time: Fri Oct 10:15:22 gmt+0800

3, GetDate () method returns one day of the month

One day in the month refers to the use of local time, whose return value is an integer between 1 ~ 31.

Copy Code code as follows:
var mydate = new Date ();
document.write (Mydate.getdate ());

Output: 26

Copy Code code as follows:
var mydate = new Date ("July 21, 1983 01:15:00");
document.write (Mydate.getdate ());

Output: 21

4. The Getday () method returns a number that represents the day of the week, and its range of values: 0--6

Copy Code code as follows:
var mydate = new Date ("July 21, 1983 01:15:00");
document.write (Mtdate.getday ());

Output 4

For this reason we can use the Date object and the array object in a way to display time humanization, the following method is very common

Copy Code code as follows:
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, returns the value is 0 (January) to 11 (December) between an integer

Similar to Getday (), we also use a combination of the array object

Copy Code code as follows:
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 4-digit number that represents the year.

The return value is a four-digit number that represents the full year, including the century value, rather than the abbreviated two-digit form.

Copy Code code as follows:
var d = new Date ();
document.write (D.getfullyear ());

Output:
2012

Copy Code code as follows:
var born = new Date ("July 21, 1983 01:15:00");
document.write ("I was born in" + Born.getfullyear ());

Output:
1983

Through the above methods three methods combined we can draw a more humane time display, example:

Copy Code code as follows:
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 ()]);

The output is:
Date is October Friday

7. The GetHours () method returns the hour field of time, which returns an integer between 0 (midnight) and 23 (Evening 11 o'clock).

Copy Code code as follows:
var born = new Date ("July 21, 1983 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, and if the value is less than 10, only one digit is returned.

8. The getminutes () method returns the minute field of time, and the return value is an integer between 0 ~ 59.

Similar to the above method, the return value is not always two digits, and if the value is less than 10, only one digit is returned.

9, the Getseconds () method can return the time of the second, the return value is 0 ~ 59 of an integer.

Similar to the above method, the return value is not always two digits, and if the value is less than 10, only one digit is returned.

10, Getmilliseconds () method can return the time of the millisecond, millisecond field, displayed in local time. The return value is an integer between 0 ~ 999.

Note: The value returned by Getmilliseconds () is a three-digit number.

However, the return value is not always three digits, and if the value is less than 100, only a two-digit number is returned, and if the value is less than 10, only one digit is returned.
Here's a couple of ways to show time:
---> Way One

Copy Code code as follows:
var d = new Date ();
document.write (' Time is ' +d.gethours () + ":" +d.getminutes () + ":" +d.getseconds ());

Output: Time is 10:52:2, it is obvious that only one bit is displayed in seconds <10, others are similar, so it is recommended that the second display method

---> Mode II

Copy Code code as follows:
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 January 1, 1970 of the local time, so you can customize date ("June 26 2000");

Copy Code code as follows:
var d = new Date ();
document.write (D.gettime () + "milliseconds since 1970/01/01");

Output: 1350615452813 milliseconds since 1970/01/01

So we can get the local time from 1970/01/01 years from this number

Copy Code code as follows:
var minutes = 1000*60;//has 60 seconds in one minute, 60 milliseconds in a second, and so on
var hours = minutes*60;
var days = hours*24;
var years = days*365;
var d = new Date ();
var t = d.gettime ();//Get the number of milliseconds from 1970/01/01
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: The year is a decimal number and can be changed to an integer (in fact, there is no integer difference in JavaScript).

The last line is modified to:

Copy Code code as follows:
document.write ("It ' s been:" + parseint (y) + "years since 1970/01/01!");

Output:
It ' s been:42 years since 1970/01/01!
parseint (float); You can convert a floating-point type to an integral type

12. The getTimezoneOffset () method returns the difference between GMT and local time, in minutes.

Note:
the getTimezoneOffset () method returns the number of minutes between local time and GMT time or UTC time.
In fact, the function tells us the time zone in which the JavaScript code is running, and whether daylight savings times are specified.
The return is in minutes, not in hours, because some countries occupy a time zone that is less than one hours apart.

Copy Code code as follows:
var d = new Date ();
document.write (D.gettimezoneoffset ());

Output:
-480 (East Eight District, 8*60)

So you can judge your time zone by this method

Copy Code code as follows:
function Checkzone (Zone)
{
if (zone==0)
{
Return "0 time zone";
}
else if (zone>0)
{
Return "West" +parseint (ZONE/60) + "district";
}
Else
{
Return "East" +parseint (Math.Abs (ZONE/60)) + "District";
}
}
var d = new Date ();
document.write (Checkzone (D.gettimezoneoffset ()));

where Math.Abs () is the absolute value
The above method is oneself original, because the geography is not good, do not know right, beg correct

13. The parse () method resolves a date-time string and returns the number of milliseconds from the 1970/1/1 midnight distance from that date time.

Parse (str); STR is a string, a string that matches the time format

Copy Code code as follows:
var minutes = 1000 * 60;
var hours = minutes * 60;
var days = hours * 24;
var years = days * 365;
var t = date.parse ("June 8, 2005");
var y = t/years;
document.write ("It s been:" + parseint (y) + "years from 1970/01/01");
document.write ("to 2005/07/08!");

The output is:
It ' s been:35 years from 1970/01/01 to 2005/07/08!

14. The Setdate () method is used to set a day of one months.

Setdate (day) is required by day. Represents a number (1 ~ 31) of a day in a one-month period.
Note, this method is modified on the basis of the original object, changing the value of the original data
Example:

Copy Code code as follows:
var d = new Date ();
document.write (D.getdate () + "<br>");
D.setdate (31);
document.write (D.getdate ());

Output:
19
31

15, Setmonth () method is used to set the month, and Setdate () using the method has been, will also change the original Date object

Setmonth (Month,day), the second parameter may not currently be supported, month required. A numeric value representing the month between 0 (January) ~ 11 (December).
If the day argument is supported, days represents the number of days of the month, between 1 and 31. expressed in local time.

Copy Code code as follows:
var d = new Date ();
document.write (D.getmonth () + "" +d.getdate () + "<br>");
D.setmonth (11,26);
document.write (D.getmonth () + "" +d.getdate ());

The output is:
9 19
11 26

16. The setFullYear () method is used to set the year.

setFullYear (Year,month,day);
Year required. A four-bit integer representing the year. expressed in local time.
Month optional. Represents the number of months, between 0 ~ 11. expressed in local time.
Day is optional. Represents the number of a day in the month, between 1 ~ 31. expressed in local time.

Copy Code code as follows:
var d = new Date ();
D.setfullyear (1992,10,3);
document.write (d);

The output is:
Tue Nov 1992 11:31:58 gmt+0800

17. The sethours () method is used to set the hour field for the specified time.

Sethours (HOUR,MIN,SEC,MILLISEC);
Hour required. Represents the number of hours, between 0 (midnight) ~ 23 (Evening 11 o'clock), in local time (below).
Min can choose. Represents the number of minutes, between 0 ~ 59. This parameter is not supported until Emcascript is normalized.
The SEC is optional. The number that represents the second, between 0 ~ 59. This parameter is not supported until Emcascript is normalized.
Millisec Optional. A number that represents milliseconds, between 0 ~ 999. This parameter is not supported until Emcascript is normalized.

Copy Code code as follows:
var d = new Date ()
D.sethours (15,35,1)
document.write (d)

The output is:
Fri Oct 15:35:01 utc+0800 2012

18. The Setminutes () method is used to set the minute field for the specified time.

Setminutes (MIN,SEC,MILLISEC)
Min is required. Represents the number of minutes, between 0 ~ 59, in local time (below).
The SEC is optional. The number that represents the second, between 0 ~ 59. This parameter is not supported until Emcascript is normalized.
Millisec Optional. A number that represents milliseconds, between 0 ~ 999. This parameter is not supported until Emcascript is normalized.

Copy Code code as follows:
var d = new Date ()
D.setminutes (1)
document.write (d)

The output is:
Fri Oct 11:01:11 utc+0800 2012

19. The Setseconds () method is used to set the second field for the specified time.

Setseconds (SEC,MILLISEC)
SEC required. A value that represents the number of seconds, which is an integer between 0 ~ 59.
Millisec Optional. A number that represents milliseconds, between 0 ~ 999. This parameter is not supported until Emcascript is normalized.

20. The Setmilliseconds () method is used to set the millisecond field for the specified time.

Setmilliseconds (MILLISEC)
Millisec required. Used to set the Dateobject millisecond field, which is an integer between 0 ~ 999.

21, the SetTime () method sets the Date object in milliseconds.

This method is a more common method of storing date.gettime () milliseconds in the database.
How to show it when you return it, is to use this method
SetTime (MILLISEC)
Millisec required. The date and time to set according to the number of milliseconds between midnight GMT time 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. Representing a date in milliseconds can make it independent of the time zone.

Copy Code code as follows:
var d = new Date ();
D.settime (77771564221);
document.write (d);

The output is:
Mon June 1972 11:12:44 gmt+0800
This method can be converted to a time object based on a long data type stored in the database

22. The totimestring () method converts the time portion of the Date object to a string and returns the result.

Copy Code code as follows:
var d = new Date ();
document.write (D.totimestring ());

Output:
11:50:57 gmt+0800

23. The todatestring () method converts the date part of the dates object to a string and returns the result.

Copy Code code as follows:
var d = new Date ();
document.write (D.todatestring ());

Output:
Fri OCT 19 2012

24, there are many ways to design a UTC time zone, which is not listed here, is simply a summary of the general more common methods

See more JavaScript syntax, you can focus on: JavaScript reference tutorial, JavaScript Code style guide, and also hope that many people support the cloud-Habitat community.

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.