Time to set the JavaScript date object

Source: Internet
Author: User
Tags date1 set time

Before the Date object JS always feel familiar with, and do not want to delve into its why, so whenever his real application up, always confused, today spent 2 hours of time to study carefully, feel enlightened, so, to this record, one for later review, and give me have the feeling of comrades For reference (code copy to local test):

JavaScript Date Object--Set time:

Test code:

<!    DOCTYPE html>Body {background-color: #333; color:white;} </style>/** * * 2. Set the time*/        varDate1 =NewDate (); Date1.setdate (15);//setDate () sets the day of the month in the Date object (1 ~ 31). "Syntax: Dateobject.setdate (day)"        varDate2 =NewDate (); Date2.setmonth (4);//setmonth () sets the month (0 ~ 11) in the Date object. "Syntax: Dateobject.setmonth (Month,day (*))" NOTE: (*) indicates optional        varDate3 =NewDate (); Date3.setfullyear (2014);//setFullYear () sets the year (four digits) in the Date object. "Syntax: Dateobject.setfullyear (Year,month (*), Day (*))" NOTE: (*) indicates optional        varDate4 =NewDate (); Date4.setyear (2015);//please use the setFullYear () method instead.         varDate5 =NewDate (); Date5.sethours (5);//sethours () sets the hour (0 ~ 23) in the Date object. "Syntax: Dateobject.sethours (hour,min (*), SEC (*), millisec (*))" NOTE: (*) indicates optional        varDate6 =NewDate (); Date6.setminutes (34);//setminutes () sets the minute (0 ~ 59) in the Date object. "Syntax: Dateobject.setminutes (MIN,SEC (*), millisec (*))" NOTE: (*) indicates optional        varDate7 =NewDate (); Date7.setseconds (50);//setseconds () sets the second (0 ~ 59) in the Date object. "Syntax: Dateobject.setseconds (SEC,MILLISEC (*))" NOTE: (*) indicates optional        varDate8 =NewDate (); Date8.setmilliseconds (888);//setmilliseconds () sets the milliseconds (0 ~ 999) in the Date object. "Syntax: Dateobject.setmilliseconds (millisec" )                varDate9 =NewDate (); Date9.settime (77771564221);//the SetTime () method sets the Date object in milliseconds. The number of milliseconds to set the date and time, according to the GMT time, between midnight January 1, 1970. 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.                 varDate10 =NewDate (); Date10.setutcdate (15);//setUTCDate () sets the day of the month in the Date object according to the world time (1 ~ 31).         varDate11 =NewDate (); Date11.setutcmonth (11);//setUTCMonth () sets the month (0 ~ 11) in the Date object according to the world time.         varDate12 =NewDate (); Date12.setutcfullyear (2015);//setUTCFullYear () sets the year (four digits) in the Date object according to the world time.         varDate13 =NewDate (); Date13.setutchours (22);//setutchours () sets the hour (0 ~ 23) in the Date object according to the world time.         varDate14 =NewDate (); Date14.setutcminutes (2);//setUTCMinutes () sets the minute (0 ~ 59) in the Date object according to the world time.         varDATE15 =NewDate (); Date15.setutcseconds (15);//setUTCSeconds () sets the seconds (0 ~ 59) in the Date object according to the world time.         varDate16 =NewDate (); Date16.setutcmilliseconds (666);//setUTCMilliseconds () sets the milliseconds (0 ~ 999) in the Date object according to the world time. document.write (' date.setdate==== ' +date1+ ' <br><br> '); document.write (' date.setmonth==== ' +date2+ ' <br><br> '); document.write (' date.setfullyear==== ' +date3+ ' <br><br> '); document.write (' date.setyear==== ' +date4+ ' <br><br> '); document.write (' date.sethour==== ' +date5+ ' <br><br> '); document.write (' date.setminute==== ' +date6+ ' <br><br> '); document.write (' date.setsecond==== ' +date7+ ' <br><br> '); document.write (' date.setminllisecond==== ' +date8.getmilliseconds () + ' <br><br> '); document.write (' date.settime==== ' +date9+ ' <br><br> '); document.write (' date.setutcdate==== ' +date10+ ' <br><br> '); document.write (' date.setutcmonth==== ' +date11+ ' <br><br> '); document.write (' date.setutcfullyear==== ' +date12+ ' <br><br> '); document.write (' date.setutchours==== ' +date13+ ' <br><br> '); document.write (' date.setutcminutes==== ' +date14+ ' <br><br> '); document.write (' date.setutcseconds==== ' +date15+ ' <br><br> '); document.write (' date.setutcmilliseconds==== ' +date16.getmilliseconds () + ' <br><br> '); </script></body>

Test results:

Time to set the JavaScript 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.