Summary of JavaScript time operations

Source: Internet
Author: User
Date object Introduction

JS has objects dedicated to date and time processing.

Date object attributes:

Attribute Description FF IE
Constructor Returns a reference to the date function that creates this object. 1 4
Prototype Allows you to add attributes and methods to objects. 1 4

Date object method:

Method Description FF IE
Date () Returns the date and time of the current day. 1 3
Getdate () Returns a day of a month from the date object (1 ~ 31 ). 1 3
Getday () Returns a day of the week from the date object (0 ~ 6 ). 1 3
Getmonth () Returns the month from the date object (0 ~ 11 ). 1 3
Getfullyear () Returns the year from the date object with four digits. 1 4
Getyear () Use the getfullyear () method instead. 1 3
Gethours () Returns the hour of the date object (0 ~ 23 ). 1 3
Getminutes () Returns the minute of the date object (0 ~ 59 ). 1 3
Getseconds () Returns the number of seconds of the date object (0 ~ 59 ). 1 3
Getmilliseconds () Returns the millisecond of the date object (0 ~ 999 ). 1 4
Gettime () Returns the number of milliseconds since January 1, January 1, 1970. 1 3
Gettimezoneoffset () Returns the minute difference between the local time and Greenwich Mean Time (GMT. 1 3
Getutcdate () Returns the Day (1 ~ 31 ). 1 4
Getutcday () Returns the day of the week (0 ~ 6 ). 1 4
Getutcmonth () Returns the month (0 ~ 11 ). 1 4
Getutcfullyear () Returns the four-digit year from the date object based on the universal time. 1 4
Getutchours () Returns the hour of the date object based on the Universal Time (0 ~ 23 ). 1 4
Getutcminutes () Returns the minute of the date object based on the Universal Time (0 ~ 59 ). 1 4
Getutcseconds () Returns the second of the date object based on the Universal Time (0 ~ 59 ). 1 4
Getutcmilliseconds () Returns the millisecond of the date object based on the Universal Time (0 ~ 999 ). 1 4
Parse () Returns the number of milliseconds from midnight, January 1, January 1, 1970 to the specified date (string. 1 3
Setdate () Set a day of the month in the date object (1 ~ 31 ). 1 3
Setmonth () Set the month (0 ~ 11 ). 1 3
Setfullyear () Set the year (four digits) in the date object ). 1 4
Setyear () Use setfullyear () instead. 1 3
Sethours () Set the hour in the date object (0 ~ 23 ). 1 3
Setminutes () Set the minute in the date object (0 ~ 59 ). 1 3
Setseconds () Set the second in the date object (0 ~ 59 ). 1 3
Setmilliseconds () Set the millisecond in the date object (0 ~ 999 ). 1 4
Settime () Set the date object in milliseconds. 1 3
Setutcdate () Set the day of the month in the date object according to the Universal Time (1 ~ 31 ). 1 4
Setutcmonth () Set the month in the date object according to the Universal Time (0 ~ 11 ). 1 4
Setutcfullyear () Set the year (four digits) in the date object according to the Universal Time ). 1 4
Setutchours () Set the hour in the date object according to the Universal Time (0 ~ 23 ). 1 4
Setutcminutes () Set the minute in the date object according to the Universal Time (0 ~ 59 ). 1 4
Setutcseconds () Set the second in the date object according to the Universal Time (0 ~ 59 ). 1 4
Setutcmilliseconds () Set the millisecond in the date object according to the Universal Time (0 ~ 999 ). 1 4
Tosource () Returns the source code of the object. 1 -
Tostring () Converts a date object to a string. 1 4
Totimestring () Converts the time part of the date object to a string. 1 4
Todatestring () Converts the date part of the date object to a string. 1 4
Togmtstring () Use the toutcstring () method instead. 1 3
Toutcstring () Converts a date object to a string based on the universal time. 1 4
Tolocalestring () Converts a date object to a string based on the local time format. 1 3
Tolocaletimestring () Converts the time part of the date object to a string based on the local time format. 1 3
Tolocaledatestring () Converts the date part of the date object to a string based on the local time format. 1 3
UTC () Returns the number of milliseconds from January 1, January 1, 1970 to the specified date based on the universal time. 1 3
Valueof () Returns the original value of the date object. 1 4

Get date and time

1. Get the current date and time of the system

var date= new Date();

2. Obtain the specified date Based on the date string.

Method 1: new date (mm/DD/YYYY)

var date= new Date("05/15/2013");

Method 2: New Date (MM-DD-YYYY)

var date= new Date("05-15-2013");

Method 3: new date (yyyy/mm/DD) (Note: new date (YYYY-MM-DD) This method does not work)

var date= new Date("2013/05/15");

Method 4: setfullyear (yyyy, mm, DD) (Note: The month ranges from 0 to 11. Therefore, to get February, set the month value to 04. )

var date= new Date();date.setFullYear(2013,04,15);

Addition and subtraction of dates

1. Increase or decrease the number of days. Use the getdate () + adddays Method

var date=new Date()date.setDate(date.getDate()+5)

Note: This method automatically handles changes in the month and year. (For example, if you add 30 days, it will be one month .)
This method can also directly add a function to the date object:

<script>var date=new Date()Date.prototype.addDays = function(days) {this.setDate(this.getDate()+days);} date.addDays(5);</script>

2. Increase or decrease the number of months. Use the getmonth () + addmonths Method

The processing method is similar to the number of days. The year change will also be automatically processed.

var date=new Date()date.setMonth(date.getMonth()+10);

3. Weekly and quarterly Processing

Weekly processing can be attributed to daily processing. Week * 7

Quarter processing can be returned to month processing at quarter * 3

Based on the above, you can add functions like adddate to date for unified processing.

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.