The Date object is a date and time object. Its main function is to implement the processing of date and time.
1. Create a Date object
The code copy is as follows: var myDate = new Date();
or
Copy the code as follows: var myDate = new Date("July 21, 1983 01:15:00");//This method is a custom date and time method. If the format is incorrect, it is a prompt for Invalid Date.
The constructor attribute of the Date object is: Date
document.write(myDate.constructor == Date);//Output true
2. The Date() method returns today's date and time, and the format is a fixed format:
The code copy is as follows: document.write(Date());//Date() is a built-in object in Javascript and can be used directly
Output:
Fri Oct 26 2012 10:15:22 GMT+0800
Format: Week Month Date Year Time Zone
Also: If you customize a Date() object, the return result is the same
The code copy is as follows: var myDate = new Date();
document.write(myDate);
Fri Oct 26 2012 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 at will.
Example, document.write(Date("July 21, 1983 01:15:00"));
It shows the time still the current time: Fri Oct 26 2012 10:15:22 GMT+0800
3. The getDate() method returns a certain day of the month
A day in the month refers to the use of local time, and its return value is an integer between 1 and 31.
The code copy is as follows: var myDate = new Date();
document.write(myDate.getDate());
Output: 26
The code copy is as follows: var myDate = new Date("July 21, 1983 01:15:00");
document.write(myDate.getDate());
Output: 21
4. The getDay() method can return a number representing a day of the week, and its value range is: 0--6
The code copy is as follows: var myDate = new Date("July 21, 1983 01:15:00");
document.write(mtDate.getDay());
Output 4
For this purpose, we can use the combination of Date object and Array object to display time humanized. The following method is very common.
The code copy is 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, and the return value is an integer between 0 (January) and 11 (December)
Similar to getDay(), we also use the method combined with Array objects.
The code copy is 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 can return a 4-digit number representing the year.
The return value is a four-digit number that represents the full year including the century value, rather than the abbreviation of the double-digit number.
The code copy is as follows: var d = new Date();
document.write(d.getFullYear());
Output:
2012
The code copy is as follows: var born = new Date("July 21, 1983 01:15:00");
document.write("I was born in " + born.getFullYear());
Output:
1983
By combining the above three methods, we can obtain a more humane time display, such as:
The code copy is 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 2012 October 19 Friday
7. The getHours() method can return the hour field of time, and the return value is an integer between 0 (midnight) and 23 (11 pm).
The code copy is 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. If the value is less than 10, only one digit will be returned.
8. The getMinutes() method can return the minute field of time, and the return value is an integer between 0 and 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 second of the time, and the return value is an integer between 0 and 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.
10. The getMilliseconds() method can return the milliseconds and milliseconds fields of the time, and display it in local time. The return value is an integer between 0 and 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 will be returned. If the value is less than 10, only one digit will be returned.
Here are two ways to display time:
--->Method One
The code copy is as follows: var d = new Date();
document.write('Time is '+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
Output: Time is 10:52:2, the disadvantage is obvious. Only one is displayed when the second is <10, and the others are also similar. Therefore, the second display method is recommended.
--->Method 2
The code copy is 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 can return the number of milliseconds between the local time and January 1, 1970, so you can customize Date("Jul 26 2000");
The code copy is as follows: var d = new Date();
document.write(d.getTime() + " millionseconds since 1970/01/01");
Output: 1350615452813 millionseconds since 1970/01/01
Therefore, we can obtain the number of years of the local time distance of 1970/01/01 based on this number
The code copy is as follows: var minutes = 1000*60;//There are 60 seconds per minute and 60 milliseconds per second, and the following is analogy
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 distance 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 at this time is a decimal, you can change it to an integer (in fact, there is no difference between integers in Javascript)
The last line is modified to:
The code copy is 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); can convert floating point types to integers
12. The getTimezoneOffset() method can return the time difference between Greenwich time and local time in minutes.
Note:
The getTimezoneOffset() method returns the number of minutes that differs between local time and GMT time or UTC time.
In fact, this function tells us the time zone where the JavaScript code is run and whether the specified time is daylight saving time.
Returns are counted in minutes, not in hours, because some countries occupy time zones that are less than an hour interval.
The code copy is as follows: var d = new Date();
document.write(d.getTimezoneOffset());
Output:
-480 (East Eighth District, 8*60)
Therefore, the time zone can be judged according to this method
The code copy is as follows: 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()));
Where Math.abs() is to find the absolute value
The above method is original by myself, because the geography is not good, I don’t know if it is right, so I’ll ask for corrections
13. The parse() method can parse a date and time string and return the number of milliseconds from midnight to the date and time at 1970/1/1.
parse(str);str is a string that conforms to the time format
The code copy is as follows: 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!");
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 certain day of a month.
setDate(day), required on day. A value representing a day of the month (1 ~ 31).
Note, this method is modified based on the original object, which will change the value of the original data.
example:
The code copy is as follows: var d = new Date();
document.write(d.getDate()+"<br>");
d.setDate(31);
document.write(d.getDate());
Output:
19
31
15. The setMonth() method is used to set months. It will also change the original Date object when used with setDate() method.
setMonth(month,day), the second parameter may not be supported at present, month is required. A value representing the month, which is between 0 (January) and 11 (December).
If the day parameter is supported, day represents the value of a certain day in the month, between 1 and 31. Expressed in local time.
The code copy is 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-digit integer representing the year. Expressed in local time.
month is optional. The value of the month is between 0 and 11. Expressed in local time.
day optional. The value representing a certain day in the month, between 1 and 31. Expressed in local time.
The code copy is as follows: var d = new Date();
d.setFullYear(1992,10,3);
document.write(d);
The output is:
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 Required. The value representing the hour is between 0 (midnight) and 23 (11:00 pm), calculated in local time (same below).
min optional. The value representing minutes, between 0 and 59. This parameter is not supported until EMCAScript is standardized.
sec is optional. The value representing the second, between 0 and 59. This parameter is not supported until EMCAScript is standardized.
millisec optional. The value representing milliseconds is between 0 and 999. This parameter is not supported until EMCAScript is standardized.
Copy the code as follows: var d = new Date()
d.setHours(15,35,1)
document.write(d)
The output is:
Fri Oct 19 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 Required. The value representing minutes, ranging from 0 to 59, is calculated in local time (same below).
sec is optional. The value representing the second, between 0 and 59. This parameter is not supported until EMCAScript is standardized.
millisec optional. The value representing milliseconds is between 0 and 999. This parameter is not supported until EMCAScript is standardized.
Copy the code as follows: var d = new Date()
d.setMinutes(1)
document.write(d)
The output is:
Fri Oct 19 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 representing a second, which is an integer between 0 and 59.
millisec optional. The value representing milliseconds is between 0 and 999. This parameter is not supported until EMCAScript is standardized.
20. The setMilliseconds() method is used to set the milliseconds field of the specified time.
setMilliseconds(millisec)
millisec Required. Used to set the dateObject millisecond field, which is an integer between 0 and 999.
21. The setTime() method sets the Date object in milliseconds.
This method is a relatively common method, storing Date.getTime() milliseconds in the database.
How to display it when returning, this method is used
setTime(millisec)
millisec Required. The date and time to set is based on the number of milliseconds between midnight on 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 makes it independent of the time zone.
The code copy is as follows: var d = new Date();
d.setTime(77771564221);
document.write(d);
The output is:
Mon Jun 19 1972 11:12:44 GMT+0800
This method can convert it into a time object based on the long data type stored in the database.
22. The toTimeString() method can convert the time part of the Date object into a string and return the result.
The code copy is as follows: var d = new Date();
document.write(d.toTimeString());
Output:
11:50:57 GMT+0800
23. The toDateString() method can convert the date part of the Date object into a string and return the result.
The code copy is as follows: var d = new Date();
document.write(d.toDateString());
Output:
Fri Oct 19 2012
24. There are many methods for designing UTC time zones, which are not listed here, but are just a summary of the general and more common methods.
To view more JavaScript syntax, you can follow: "JavaScript Reference Tutorial" and "JavaScript Code Style Guide". I also hope that everyone will support Wulin.com more.