Home  >  Article  >  Web Front-end  >  JavaScript uses regular expressions to remove "-" from dates_javascript tips

JavaScript uses regular expressions to remove "-" from dates_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:42:451507browse

1. Description

It often happens that the date format of the page is: YYYY-MM-DD, while the date format in the database is: YYYYMMDD. The two need to be converted before they can be sent to the Java background for query data.

Usually, there are two methods for this conversion. The first is to intercept the date string and then concatenate it; the second is to use regular expressions to remove "-"

In comparison, the second method is faster and less error-prone.

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去除日期中的“-”</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("替换之前的日期:" + date); 
//替换“-” 
var dateStr = date.replace(/\-/g, ""); 
alert("替换之后的日期:" + dateStr); 
} 
</script> 

</head> 

<body> 
<input type="button" value="日期格式化" onclick="dateFormat()"/> 
</body> 
</html>


3. Achieve results

(1) During initialization

(2) After clicking "OK"

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn