Time Control precise to seconds in JSP pages

Source: Internet
Author: User

Technology: Struts1 + jsp + MSql

Requirement: The ncmgt network monitoring system displays the user's operation logs and queries the user's detailed operation records based on the operation time. The time is accurate to seconds.

The general effect is as follows ,!

You can clearly see that the red line is queried by time, and the time is accurate to seconds. It should not be manually input by the user, otherwise it will greatly reduce the user experience performance, this is just the time control. It's just like a small clock, but it's just a picture. We need to know what is hidden behind it. Let's show you the effect first, when we click a small clock, the following pop-up is displayed:

In such a calendar image, you can see that the bottom line is added with the hour, minute, and second. The default is none.

The following describes how to achieve this effect.

Step 1:

Introduce js files

The header of the JSP page

 
 
  1. <script language="javascript" src="http://localhost:8080/ncmgt//js/calendar.js"></script> 

Step 2:

Prepare a small image.

 
 
  1. <! -- The text box is used to receive the time selected by the user and pass it to the oplog object class attribute in the form -->
  2.  
  3. <Input type = "text" name = "oplogSearch. dateBegin" size = "16" value = "" readonly = "readonly" class = "textarea1">
  4. <A href = "javascript: buildCal (oplogListForm. elements ['oplogsearch. datebegin'], true)">
  5. </a>

When you select a time, the following function puts the time in the text box and submits the form.

 
 
  1. Function closewin (){
  2. // Obtain the login name and time
  3. Parent.doc ument. forms ["0"]. elements ["oplogSearch. opname"]. value
  4. = Document. forms ["0"]. elements ["oplogSearch. opname"]. value;
  5. Parent.doc ument. forms ["0"]. elements ["oplogSearch. dateBegin"]. value
  6. = Document. forms ["0"]. elements ["oplogSearch. dateBegin"]. value;
  7. Parent.doc ument. forms ["0"]. elements ["oplogSearch. dateEnd"]. value
  8. = Document. forms ["0"]. elements ["oplogSearch. dateEnd"]. value;
  9. Parent.doc ument. forms ["0"]. submit ();
  10. Parent.doc ument. getElementById ('divwindow'). style. display = "none ";


;

  • Return false;
  • }

It is not a matter of submitting a form. It is very important to have a process. If the user chooses a time from 5.3 to 5.1. That is, what we often say is greater than, smaller than, small.

This is not logical. It is like a number greater than 3 and less than 1. Where is this? To prevent such a value from being transferred to the background Action, we should process it after the user selects the time.

 
 
  1. Function comparePair (){
  2. Var begin, end;
  3. Begin = document. forms ["0"]. elements ["oplogSearch. dateBegin"]. value;
  4. End = document. forms ["0"]. elements ["oplogSearch. dateEnd"]. value;
  5. If (begin> end & end! = ""){
  6. Alert ("the time you selected is incorrect! ');
  7. Return false;
  8. }
  9. Return true;
  10. }

In this way, it will be processed in the background. Another point is that the MySql database I use cannot use to_date () for the processing time in mySQL. It is the time and date function in Oracle, we can use Date. format ();

For example:

 
 
  1. select count(*) from nc_op_log where 1=1    and date <= DATE_FORMAT('2012-05-03 1
  2. 7:28:39','YYYY-MM-DD HH24:MI:SS')  and date >= DATE_FORMAT('2012-05-01 17:28:43','YYYY-MM-DD HH24:MI:SS')  

However, if your field is of the Date type and does not need any function, you can directly query the result by string processing.

For example:

 
 
  1. select count(*) from nc_op_log where 1=1  and date >= '2012-05-01 17:28:43'   and date <= '2012-05-03 17:28:39'  

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.