[JS] Note 13 Date Object

Source: Internet
Author: User
Tags date1

-How to get and set time
Use date objects to make the corresponding effects


1. Set the time
Create a Time object

New Date (time);

Set time

From January 1, 1970 to
Several formats:
New Date ("May 25, 2024");
New Date ("2015/12/25,12:20:12");
New Date (2016,1,1); February 1, 2016 zero

1<script>2 /*Create a Time object*/3     varnow=NewDate ();4Console.log (now);//Tue Sep 14:47:44 gmt+0800 (China Standard Time-->CST)5 /*Set the time*/6     vardate1=NewDate (' may 25,2024 ');//May 24 20147Console.log (date1);//Sat may 2024 00:00:00 gmt+0800 (China Standard Time)8     varDate2=NewDate ("2016/10/01,10:10:11");//Month day, time division seconds9Console.log (DATE2);//Sat Oct 10:10:11 gmt+0800 (China Standard Time)Ten     vardate3=NewDate (2016,10,1,10,10,11,111);//year, month (10 for November), day, time, minute, second, OneConsole.log (Date3);//Tue 10:10:11 gmt+0800 (China Standard Time) A     // -</script>

2. Get the time of the current time zone

Time includes more than one message, how do you get one of them?

--method to get the current time

getFullYear (); Get a 4-digit year
getMonth (); Gets the month from 0~11,0, which represents January, 7 for August
GetDate (); Gets the number of days in the month (date)
GetDay (); Get day of the week, 0 for Sunday, 6 for Saturday

GetHours () Gets the current number of hours
Getminutes () Gets the current number of minutes
Getseconds () Gets the current number of seconds
Getmilliseconds (); Gets the current number of milliseconds
getTime (); Gets the number of milliseconds from January 1, 1970 08:00:00 to present

--Method code to get time:

<script>/*method to get Time (Date object has no attributes)*/    varnow=NewDate (); Console.log (now);//Sat Sep 20:58:13 gmt+0800 (China Standard Time)Console.log (Now.getfullyear ());// .Console.log (typeofNow.getfullyear ());// NumberConsole.log (Now.getmonth ());//month -8-->9Console.log (Now.getdate ());//Day-6Console.log (Now.getday ());//Week-2Console.log (Now.gethours ());//Time -20Console.log (Now.getminutes ());//score -58Console.log (Now.getseconds ());//sec -13Console.log (Now.getmilliseconds ());//milliseconds (0~999)Console.log (Now.gettime ());//1473512293715</script>

3. Set the time of the current time zone

setFullYear (); Set the 4-digit year
setmonth (); Set month, starting from 0, 0 for January
SetDate (); Set the number of days in a month
Sethours (); Set the current number of hours, 0-23
Setminutes (); Set the current number of minutes, 0-59
Setseconds (); Sets the current number of seconds, 0-59
Setmilliseconds (); Set the current number of milliseconds
settime (); Add the number of milliseconds to 1970/1/1

<script>varCon=document.getelementbyid (' con ')); varnow=NewDate (); Console.log (now);//Tue Sep 16:36:40 gmt+0800 (China Standard Time)Now.setfullyear (2017); Console.log (now);//Wed Sep 16:37:05Now.setmonth (6); Console.log (now);//Thu Jul 16:38:30Now.settime (1000*60*60);//January 1, 1970 in the morning 8 add the number of millisecondsConsole.log (now);//Thu Jan 1970 09:00:00    vardate1=NewDate (1000*60*60);//Current time + set number of millisecondsConsole.log (date1);//Thu Jan 1970 09:00:00</script>

4, UTC time

UTC International Standard Time also known as world time
Take the time on the 0 longitude line as the standard time for international unification. Because the 0 longitude line passes through the British Greenwich Observatory, the international Standard Time is also known as Greenwich Mean GMT.

Current GMT = International Standard Time + 8 hours

4-1. Time to get UTC time Zone

GetUTCfullyear (); Gets the 4-digit year of the UTC date
GetUTCMonth (); Get UTC Date month, starting from 0
GetUTCDate (); Get the number of days in UTC date Month
GetUTCDay (); Gets the week of the UTC date, starting from 0
GetUTCHours (); gets the number of hours of UTC date, 0-23
GetUTCMinutes (); Gets the number of minutes in UTC date, 0-59
GetUTCSeconds (); Gets the number of seconds in UTC date, 0-59
GetUTCMilliseconds (); Gets the number of milliseconds for the UTC date

4-2. Set the UTC time Zone

SetUTCFullyear (); 4-digit year with UTC date
SetUTCMonth (), setting the UTC date month, starting with 0
SetUTCDate (); Sets the number of days in UTC date Month
SetUTCHours (); Sets the number of hours of UTC date, 0-23
SetUTCMinutes (); Sets the number of minutes of UTC date, 0-59
SetUTCSeconds (); Sets the number of seconds of UTC date, 0-59
SetUTCMilliseconds (); Sets the number of milliseconds for the UTC date


5. Get the current time effect

1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title> Get current Time </title>6<style>7*{color:red;}8</style>9Ten<body> One

A<script> - varCon=document.getelementbyid (' con ')); - varTimer=NULL; theTimer=setinterval (function(){ - varnow=NewDate (); - varYear=now.getfullyear (); - varMonth=now.getmonth (+1); + varDate=now.getdate (); - varHouse=now.gethours (); + varminutes=now.getminutes (); A varSecond=now.getseconds (); at varMillise=now.getmilliseconds (); - if(house<10){ -House= ' 0 ' +House ; - } - if(minutes<10){ -Minutes= ' 0 ' +minutes; in } - if(second<10){ toSecond= ' 0 ' +second; + } - if(millise<10){ theMillise= ' 00 ' +millise; *}Else if(millise<100){ $Millise= ' 0 ' +millise;Panax Notoginseng } -Con.innerhtml= ' Beijing time: ' +year+ ' +month+ ' month ' +date+ ' +house+ ' when ' +minutes+ ' sub ' +second+ ' seconds ' +millise+ ' milliseconds '; the},1) +</script> A</body> the

6. Countdown effect

1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title> Exercise 2-Countdown </title>6<style>7*{color:red;}8</style>9Ten<body> One A -<script> -     varCon=document.getelementbyid (' con ')); the     varCon2=document.getelementbyid (' Con2 '); -     varguoq=NewDate ("2016/10/1,00:00:00"); -     varzhq=NewDate ("2016/9/15,00:00:00"); -     varEnd=guoq.gettime (); +     varEnd2=zhq.gettime (); -     varTimer=NULL, timer2=NULL; +Timer=setinterval (function(){ A         varStr1= "; at         varnow=NewDate (); -         varstart=now.gettime (); -         varzong=end-start; -         varDay=parseint (zong/(1000*60*60*24));//Acquisition Provider -         var_day=zong% (1000*60*60*24);//get remainder -         varHour=parseint (_day/(1000*60*60)); in         var_hour=_day% (1000*60*60); -         varMinutes=parseint (_hour/(1000*60)); to         var_minutes=_hour% (1000*60); +         varSeconds=parseint (_minutes/1000); -         varmillis=_minutes%1000; thestr1=day+ ' days ' +hour+ ' hours ' +minutes+ ' minutes ' +seconds+ ' seconds ' +millis+ ' milliseconds ' *Con.innerhtml=str1; $},1)Panax NotoginsengTimer2=setinterval (function(){ -         varStr1= "; the         varnow=NewDate (); +         varstart=now.gettime (); A         varzong=end2-start; the         varDay=parseint (zong/(1000*60*60*24));//Acquisition Provider +         var_day=zong% (1000*60*60*24);//get remainder -         varHour=parseint (_day/(1000*60*60)); $         var_hour=_day% (1000*60*60); $         varMinutes=parseint (_hour/(1000*60)); -         var_minutes=_hour% (1000*60); -         varSeconds=parseint (_minutes/1000); the         varmillis=_minutes%1000; -str1=day+ ' days ' +hour+ ' hours ' +minutes+ ' minutes ' +seconds+ ' seconds ' +millis+ ' milliseconds 'WuyiCon2.innerhtml=str1; the},1) -</script> Wu</body> -

Friend link address some effects can be seen: https://www.cnblogs.com/duenyang/

[JS] Note 13 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.