JavaScript learns-JS's Date object function

Source: Internet
Author: User
Tags local time

The date function of JS is very powerful. Usually used.


First get the current date:

var curdate = new Date (), var year = Curdate.getfullyear (); Year (four-digit example:) var Month = Curdate.getmonth () +1; Month 0~11var day = Curdate.getdate (); Get the number 1~31var week = Curdate.getday (); Get weeks 0~6 Explanation: 0 is Sunday 1 is Monday

So we can get to the date.

Date (). GetYear ()//returns the second digit of the year

There should not be much use for this word.


New Date ("Month dd,yyyy hh:mm:ss"), new Date ("Month dd,yyyy"), new Date (YYYY,MTH,DD,HH,MM,SS), new Date (YYYY,MTH,DD), new Date (MS);

These are the methods for creating date objects Y is the year, and M is the month. D is the day, MM is the minute, SS is the second. Say Month Remember: 0~11


Method describes the date and time that date () is returned on the day. GetDate () Returns the day of the one month (1 ~ 31) from the Date object. GetDay () Returns the day of the week (0 ~ 6) from the Date object. GetMonth () returns the month (0 ~ 11) from the Date object. getFullYear () returns the year as a four-digit number from a Date object. GetYear () Please use the getFullYear () method instead.

GetHours () returns the hour (0 ~ 23) of the Date object.

Getminutes () returns the minute (0 ~ 59) of the Date object. Getseconds () returns the number of seconds (0 ~ 59) of the Date object. Getmilliseconds () returns the milliseconds (0 ~ 999) of the Date object. GetTime () returns the number of milliseconds since January 1, 1970.

getTimezoneOffset () returns the minute difference between local time and Greenwich Mean Time (GMT). getUTCDate () Returns the day of the month from the Date object (1 ~ 31), based on the world.

GetUTCDay () Returns the day of the week (0 ~ 6) from the Date object, based on world time. getUTCMonth () returns the month (0 ~ 11) from the Date object, based on the world. getUTCFullYear () returns a four-digit year from a Date object based on world time. getUTCHours () the hour (0 ~ 23) that returns the Date object based on the world. getUTCMinutes () the minute (0 ~ 59) that returns a Date object based on the world time.

getUTCSeconds () returns the second (0 ~ 59) of the Date object, based on the world. getUTCMilliseconds () returns the milliseconds (0 ~ 999) of a Date object based on the world.

Parse () returns the number of milliseconds from midnight January 1, 1970 to the specified date (string). SetDate () Sets the day of the month in the Date object (1 ~ 31). Setmonth () sets the month (0 ~ 11) in the Date object. setFullYear () sets the year (four digits) in the Date object. Setyear () Please use the setFullYear () method instead. Sethours () sets the hour (0 ~ 23) in the Date object. Setminutes () sets the minute (0 ~ 59) in the Date object.

Setseconds () sets the second (0 ~ 59) in the Date object. Setmilliseconds () sets the milliseconds (0 ~ 999) in the Date object. SetTime () Sets the Date object in milliseconds. SetUTCDate () Sets the day of the month in the Date object (1 ~ 31) based on the world time. setUTCMonth () sets the month (0 ~ 11) in the Date object based on the world time. setUTCFullYear () sets the year (four digits) in the Date object based on the world time. setUTCHours () sets the hour (0 ~ 23) in the Date object based on the world time. setUTCMinutes () sets the minute (0 ~ 59) in the Date object based on the world time. setUTCSeconds () sets the seconds (0 ~ 59) in the Date object based on the world time.

setUTCMilliseconds () sets the milliseconds (0 ~ 999) in the Date object based on the world time.

Tosource () returns the source code of the object. ToString () Converts the Date object to a string.

toTimeString () Converts the time portion of a Date object to a string. toDateString () Converts the date part of a Date object to a string.

toGMTString () Please use the toutcstring () method instead. toUTCString () According to the world time. Converts a Date object to a string.

toLocaleString () Converts a Date object to a string based on the local time format. toLocaleTimeString () Converts the time portion of a Date object to a string, based on the local time format. toLocaleDateString () Converts the date part of a Date object to a string, based on the local time format. UTC () returns the number of milliseconds from January 1, 1970 to the specified date, based on world time. ValueOf () returns the original value of the Date object.

These are the references: W3school (http://www.w3school.com.cn/jsref/jsref_obj_date.asp)

Assuming you need to use it directly ~ ~

Here another inference is whether the function of a leap year I wrote myself:

function Getboolyear (year) {var boolyear;if (year%400 = = 0 | | (year%100! = 0 && year%4 = = 0))) {boolyear = true;} Else{boolyear = false;} return boolyear;}
Depending on whether it is a leap year, you can also know how many days a month is. Functions such as the following:

function Getmonthday (month, boolyear) {var monthday;switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:mont Hday = 31;break;case 4:case 6:case 9:case 10:case 11:monthday = 30;break;case 2:if (Boolyear = true) {MonthDay = 29;break ;} Else{monthday = 28;break;}} return monthday;}

Usually this is it ~ ~

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

JavaScript learns-JS's Date object function

Related Article

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.