Introduction to JavaScript • Dynamic clock, show complete some methods, New Year countdown _ Time and date

Source: Internet
Author: User
Tags getdate time and date
The time object as a very important object, for us to learn. NET, not very important, but this does not mean that we can ignore, in fact, when the need is still a lot of, if completely dependent on the JS processing time, it will be problematic, because JS always assume that the time on the local machine is correct. There is another reason, he always according to GTM City to measure. We simply return a copy of the current date object, which, even if modified, does not have any effect on the object itself.

Demo One: Dynamic clock (a complex)

11:55:13
Demo Two: Some ways to show the complete (in fact, I hate some formatting)

Mon Oct 1 22:35:25 utc+0800 2007
It's been 1191249325859 milliseconds since 1970-01-01.
Back to current year 2007
2007
Return to the current month 91 because the month is 0-11, so add 1
Back to current date 1
Back to Current Week 1
Returns the current hour 22
Returns the current minute 35
Returns the current second 25
Wed Aug 11:55:03 utc+0800 2006
It's been 1155700503156 milliseconds since 1970-01-01.
Back to current year 2006
2006
Return to the current month 71 because the month is 0-11, so add 1
Back to current date 16
Back to Current Week 3
Returns the current hour 11
Returns the current minute 55
Returns the current second 3


Demo Three: Countdown

90 days, 01 hours, 24 minutes and 34 seconds from 2006 New Year! 136 days, 12 hours, 04 minutes and 56 seconds from 2006 New Year!



<% @LANGUAGE = "JAVASCRIPT" codepage= "936"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
The date</title> of <title>js core object
<script type= "Text/javascript" >
function StartTime ()
{
var today=new date ()//defines a time object
var h=today.gethours ()//define Hours
var m=today.getminutes ()//define Minutes
var s=today.getseconds ()//define Seconds
Add a zero in front of numbers<10
M=checktime (m)/Send to checktime for processing
S=checktime (s)//seconds to checktime processing
document.getElementById (' txt '). innerhtml=h+ ":" +m+ ":" +s//in layer txt display
T=settimeout (' StartTime () ', 500)//Key sentence, the United States every 500 milliseconds to run this function once
}

function Checktime (i)
{//This function is very simple, is to achieve the effect of 01-09
if (i<10)//
{i= "0" + i}
return I
}
</script>

<body onload= "StartTime ()" >
<p> time object as a very important object, For us to learn. NET, not very important, but this does not mean that we can ignore, in fact, when the need is still a lot of, if completely dependent on the JS processing time, it will be problematic, because JS always assume that the time on the local machine is correct. There is another reason, he always according to GTM City to measure. We simply return a copy of the current date object, which, even if modified, does not have any effect on the object itself. </p>
<p><strong> Demo One: Dynamic clock </strong> (a complex) </p>
<div id= "TXT" ></div>
<p><strong> Demo Two: Some ways to show complete (in fact I hate some format) </strong></p>
<p>
<script language= "JavaScript" >
var md=new Date ()
document.write (md+ "<br>")
document.write ("From 1970-01-01 to Now" +md.gettime () + "millisecond <br>")
document.write ("Back to Current Year" +md.getyear () + "<br>")
document.write (Md.getfullyear () + "<br>")
document.write ("Back to the current month" +md.getmonth () +1+ "because the month is 0-11, so add 1<br>")
document.write ("Back to Current Date" +md.getdate () + "<br>")
document.write ("Back to Current Week" +md.getday () + "<br>")
document.write ("Back to Current Hour" +md.gethours () + "<br>")
document.write ("Back to Current Minute" +md.getminutes () + "<br>")
document.write ("Back to current Second" +md.getseconds () + "<br>")
</script>
</p>
<p><strong> Demo Three: Countdown </strong></p>
<p>
<script language= "JavaScript" >
Today = new Date ()//Declare a Time object
Intdate = Today.getdate ();//Return the current day date
Inthours = Today.gethours ();//return current hour
Intminutes = Today.getminutes ()/min
Intseconds = Today.getseconds ();//sec
Intmonth = Today.getmonth () +1;//month plus 1
Intyear = Today.getyear ();/return year
Here's to get the difference of 0:0 0 seconds.
Hours = inthours;
Hours = (23-hours);
minutes = intminutes;
minutes = (59-minutes);
seconds = intseconds;
seconds = (59-seconds);

if (intyear% 4 = 0 && intyear%!= 0 | | intyear% 400 = 0)
If the current year is divided by more than 4 digits and 0 at the same time, the current year is not 0 or the current years divided by 400 remainder of 0, then this year is 366 days
{if (Intmonth = 1) {month = "from 2006 New Year"; date = (366-intdate);}
The following is the same as this sentence: Use the remaining days minus the current date number such as the next sentence, because it is February, so only 335 days, minus the current day's numbers
if (Intmonth = = 2) {month = "from 2006 New Year"; date = (335-intdate);}
}
else//otherwise for 365 days
{if (Intmonth = 1) {month = "from 2006 New Year"; date = (365-intdate);}
if (Intmonth = = 2) {month = "from 2006 New Year"; date = (334-intdate);}
}

if (Intmonth = = 3) {month = "from 2006 New Year"; date = (304-intdate);}
if (Intmonth = = 4) {month = "from 2006 New Year"; date = (273-intdate);}
if (Intmonth = = 5) {month = "from 2006 New Year"; date = (243-intdate);}
if (Intmonth = = 6) {month = "from 2006 New Year"; date = (212-intdate);}
if (Intmonth = = 7) {month = "from 2006 New Year"; date = (182-intdate);}
if (Intmonth = = 8) {month = "from 2006 New Year"; date = (152-intdate);}
if (Intmonth = = 9) {month = "from 2006 New Year"; date = (121-intdate);}
if (Intmonth = =) {month = "from 2006 New Year"; date = (91-intdate);}
if (Intmonth = =) {month = "from 2006 New Year"; date = (60-intdate);}
if (Intmonth = =) {month = "from 2006 New Year"; date = (30-intdate);}
The following of course meaning has changed, but the day, when, minutes, seconds mean bad
if (date = = 1) {date = ("0" +date+ "Day");} If the date above is 1, then add a 0 to the front.
if (date!= 1 && Date < ten && date >=0) {date = ("0" +date+ "Day");} If not equal to 1 and less than 10, both greater than or equal to 0 plus 0.
if (Date > 9) {date = (date+ "Day");} If it's greater than 9, it's not added.

if (hours ==1) {hours = ("0" +hours+ "Hour");}
if (Hours!= 1 && hours <) {hours = ("0" +hours+ "Hour");}
if (Hours > 9) {hours = (hours+ "hours");}

if (minutes = = 1) {minutes = ("0" +minutes+ "cent");}
if (minutes!= 1 && minutes <) {minutes = ("0" +minutes+ "cent");}
if (Minutes > 9) {minutes = (minutes+ "min");}

if (seconds = = 1) {seconds = ("0" +seconds+ "seconds" + "!");}
if (seconds!= 1 && seconds <) {seconds = ("0" +seconds+ "seconds!");}
if (seconds > 9) {seconds = (seconds+ "Seconds!");}
If the day is less than 0, that means the new year to the pizza
if (Date < 0) {month = "Happy";d ate = "New year!"; Hours = "New Year"; minutes = "happy"; seconds = "!";}
Here's the combination of all the values, easy.
TimeString = Month+date+hours+minutes+seconds;
document.write (timestring)
</script>
</p>
</body>

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.