JS built-in Object-date object

Source: Internet
Author: User
Tags time and date

Date object:

The data object can store any date and can be accurate to milliseconds (1/1000 seconds).

Defined:

Default initial value definition: var dataname=new Date (); /* Use keyword New;data First letter must capitalize to    make Dataname an object with initial value: Current computer system time *///Custom initial value definition: var dataname=naw Data (2016,5,25);//or: Var Dataname=new Data (' May 25,2016 ');            

A common method for working with time and date in the data object:

1.get/setdata (): Return/Set Date

1 var mydate=new Date ();//Current Time May 25, 2016 2 document.write (mydate+ "<br/>");//Output Current time
3 document.write (Mydate.getfullyear () + "<br/>");//output Current Year 4 mydate.setfullyear (16); Set Year 5 document.write (mydate+ "<br/>"); The output year is set to 0016 years
6 document.write (Mydate.getfullyear () + "<br/>");//output current year
Note: Different browser formats are different
Edge results: Wed 21:53:56 gmt+0800 (China Standard Time)
2016
Sun May, Bayi 21:53:56 gmt+0800 (China Standard Time)
16
Ie11,chrome Ibid.

2.get/setfullyear (): Returns/sets the year, expressed as a four-digit number

3.get/setyear (): Return/Set Year

4.get/setmonth (): Return/Set Month

5.get/sethours (): Return/Set //0 = January ... 11 means December, so it usually takes 1.

6.get/setminutes (): Returns/sets the number of minutes

7.get/setseconds (): Returns/sets the number of seconds

8.get/settime (): Return/Set Time (in milliseconds)

Returns/sets the time, in milliseconds, of the number of milliseconds from January 1, 1970 to 0 o'clock to the date the Date object refers to.

Example: Use the gettime () and SetTime () methods to postpone the time by 2 hours. The code is as follows:

1 var mydate=new Date (), 2 document.write ("Current time:" +mydate), 3 mydate.settime (mydate.gettima+120*60*1000); 4 document.write ("Time delayed after two hours:" +mydate);

9. Return Week Method: GetDay ()

Returns a number of 0-6 in the week, 0 representing Sunday.

If you want to return a relative "week", you can do this by using an array, getday the return value as an array subscript:

1 var mydate=new Date (); 2 var weeknum=mydate.getday (); 3 var week=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];4 document.write ("Today is:" +week[weeknum]);

JS built-in Object-date object

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.