1. Description
This happens often. The date format of the page is: YYYY-MM-DD, and the date format in the database is: YYYYMMDD. It is necessary to convert between the two before it can be transmitted to the Java background to query data.
Usually, there are two ways to convert this kind of transformation. The first is to intercept the date string first and then splice it; the second is to use regular expressions to remove "-"
In comparison, the second method is fast and not prone to errors.
2. Implement source code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>JavaScript removes "-" in date</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript"> function dateFormat() { var date = "2014-06-08"; alert("Replace before the date: " + date); //Replace "-" var dateStr = date.replace(//-/g, ""); alert("Date after the date: " + dateStr); } </script> </head> <body> <input type="button" value="dateformat" onclick="dateFormat()"/> </body> </html>3. Achieve results
(1) Initialization
(2) After clicking "OK"