JavaScript USES regular expressions to remove the in a date

  • 2020-03-30 03:15:38
  • OfStack

1, description,

It is often the case that the date format of the page is yyyy-mm-dd, while the date format in the database is YYYYMMDD, which needs to be converted between them before it can be sent to the Java background to query data.

In general, there are two ways to do this. The first is to intercept the date string and then concatenate it. The second way is to use regular expressions to remove the "-".

By comparison, the second method is fast and less prone to errors.

2, to achieve the 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 Remove the "in the 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 previous date: " + date); 
//Replace "-"
var dateStr = date.replace(/-/g, ""); 
alert(" Date after replacement: " + dateStr); 
} 
</script> 

</head> 

<body> 
<input type="button" value=" Date formatting " onclick="dateFormat()"/> 
</body> 
</html> 

3. Achieve results

(1) initialization
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201406/201406090919311.gif? 2014599200 ">  
(2) after clicking "ok"
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201406/201406090920322.gif? 20145992049 ">

Related articles: