1. Description
This happens often. The date format of the page is: YYYY-MM-DD, and the date format in the database is: YYYYMMDD. The two need to be converted before they can be passed to the Java background to query data.
Generally, 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
The code copy is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JavaScript removes "-" from 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 the date before:" + date);
//replace"-"
var dateStr = date.replace(//-/g, "");
alert("Date after replacement: " + dateStr);
}
</script>
</head>
<body>
<input type="button" value="dateformat" onclick="dateFormat()"/>
</body>
</html>
3. Achieve results
(1) Initialization
(2) After clicking "OK"