JS countdown, jump module, and comparison date jump module in JSP

Source: Internet
Author: User
Tags date now

1. Preface

This is based on some online materials re-compiled, mainly as a small accumulation of their own use, the original purpose is to make the page automatically Jump within the specified period of time, requests outside of the time range are not processed; JS and JSP code are written separately, which is put here for future reference;

 


2. Code

2.1 countdown Module

This is automatically refreshed every 1 second, and can be slightly modified for other purposes:


[Javascript]
<! -- Countdown -->
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script language = "JavaScript">
Function Dclock ()
{
Var symbol = "March 31 ";
Var deadline = new Date ("03/31/2013 11:25:00"); // Standard Time Format
Var now = new Date ();
Var diff =-480-now. getTimezoneOffset (); // time difference between Beijing time and local time
Var leave = (deadline. getTime ()-now. getTime () + diff * 60000; // getTime () can get the number of milliseconds after 1970, which is a negative number before January 1, 1970
Var day = Math. floor (leave/(1000*60*60*24); // Number of days
Var hour = Math. floor (leave/(1000*3600)-(day * 24 );
Var minute = Math. floor (leave/(1000*60)-(day * 24*60)-(hour * 60 );
Var second = Math. floor (leave/(1000)-(day * 24*60*60)-(hour * 60*60)-(minute * 60 );
 
If (now. getTime () <deadline. getTime ())
{
LiveClock. innerHTML = "now" + symbol + "and" + day + "day" + hour + "hour" + minute + "minute" + second + "second ";
Test. innerHTML = "getTime:" + now. getTime () + "Deadline:" + deadline. getTime ();
SetTimeout ("Dclock ()", 1000); // call itself every 1 second
}
Else if (now. getTime ()> deadline. getTime ())
{
LiveClock. innerHTML = "END ";
SetTimeout ("Dclock ()", 1000 );
}
}
</Script>
<! -- Countdown -->
<Body onload = Dclock ()>
<Div id = LiveClock> </div>
<Div id = test> </div>
</Body>
<! -- Countdown -->
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script language = "JavaScript">
Function Dclock ()
{
Var symbol = "March 31 ";
Var deadline = new Date ("03/31/2013 11:25:00"); // Standard Time Format
Var now = new Date ();
Var diff =-480-now. getTimezoneOffset (); // time difference between Beijing time and local time
Var leave = (deadline. getTime ()-now. getTime () + diff * 60000; // getTime () can get the number of milliseconds after 1970, which is a negative number before January 1, 1970
Var day = Math. floor (leave/(1000*60*60*24); // Number of days
Var hour = Math. floor (leave/(1000*3600)-(day * 24 );
Var minute = Math. floor (leave/(1000*60)-(day * 24*60)-(hour * 60 );
Var second = Math. floor (leave/(1000)-(day * 24*60*60)-(hour * 60*60)-(minute * 60 );

If (now. getTime () <deadline. getTime ())
{
LiveClock. innerHTML = "now" + symbol + "and" + day + "day" + hour + "hour" + minute + "minute" + second + "second ";
Test. innerHTML = "getTime:" + now. getTime () + "Deadline:" + deadline. getTime ();
SetTimeout ("Dclock ()", 1000); // call itself every 1 second
}
Else if (now. getTime ()> deadline. getTime ())
{
LiveClock. innerHTML = "END ";
SetTimeout ("Dclock ()", 1000 );
}
}
</Script>
<! -- Countdown -->
<Body onload = Dclock ()>
<Div id = LiveClock> </div>
<Div id = test> </div>
</Body> 2.2 JS jump


[Javascript]
<! -- Javascript begin -->
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script language = "JavaScript">
Function Dclock ()
{
Var deadline = new Date ("05/31/2012 20:30:00"); // time start point
Var deadline_2 = new Date ("05/31/2012 21:30:00"); // end of time
Var symbol = "May 31 ";
Var now = new Date ();
 
If (now. getTime () <deadline. getTime ())
{

}
Else if (deadline. getTime () <now. getTime () & now. getTime () <deadline_2.getTime () // c language used to write a <B <c
{
// Alert (now. getTime ());
Window. location. href = "http: // localhost/XXX/page.html ";
SetTimeout ("Dclock ()", 1000 );
}
Else if (now. getTime ()> deadline_2.getTime ())
{
 
}
}
</Script>
<Javascript end -->
<! -- Javascript begin -->
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script language = "JavaScript">
Function Dclock ()
{
Var deadline = new Date ("05/31/2012 20:30:00"); // time start point
Var deadline_2 = new Date ("05/31/2012 21:30:00"); // end of time
Var symbol = "May 31 ";
Var now = new Date ();

If (now. getTime () <deadline. getTime ())
{
 
}
Else if (deadline. getTime () <now. getTime () & now. getTime () <deadline_2.getTime () // c language used to write a <B <c
{
// Alert (now. getTime ());
Window. location. href = "http: // localhost/XXX/page.html ";
SetTimeout ("Dclock ()", 1000 );
}
Else if (now. getTime ()> deadline_2.getTime ())
{

}
}
</Script>
<Javascript end -->
2.3 JSP code

Because JS acquisition is the client time, although the server time can be obtained through interaction, it seems redundant. Here we post the JSP code


[Java]
<% @ Page contentType = "text/html; charset = UTF-8" import = "java. util. *;" %>
<% // Obtain the current time
Java. util. Date now = new java. util. Date ();
Java. util. Date dline = new java. util. Date ("03/01/2012 20:30:00 ");
Java. util. Date dline2 = new java. util. Date ("03/03/2012 21:30:00 ");
 
If (now. getTime () <dline. getTime ())
{

}
Else if (dline. getTime () <now. getTime () & now. getTime () <dline2.getTime () // c language used to write a <B <c
{
Response. setStatus (HttpServletResponse. SC _MOVED_PERMANENTLY );
String newLocn = "http: // XXX/page/earthhour /";
Response. setHeader ("Location", newLocn );
}
Else if (now. getTime ()> dline2.getTime ())
{

}
%>
 

From DesignLab
 

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.