JS string To date format, JavaScript string to date format we all know that JS is based on the results to determine the data type. Of course, we can also be converted, the following I will introduce two kinds of JS string type conversion into a date type of method, I personally prefer the first method. There are other good ways for everyone to share. 1.eval method of conversion,---recommend this method I wrote a method, everybody. Call directly
?
<script type=
"text/javascript"
>
//字符串转日期格式,strDate要转为日期格式的字符串
function getDate(strDate) {
var date = eval(
‘new Date(‘ + strDate.replace(/\d+(?=-[^-]+$)/,
function (a) {
return parseInt(a, 10) - 1; }).match(/\d+/g) +
‘)‘
);
return date;
}
//测试
alert(getDate(
"2012-05-09"
));
</script>
|
2. The second method is to use split-fraction groups. It is not recommended to use this because the date format is not flexible as follows
<script type= "Text/javascript" >// string to date format, strdate string to convert to date format function GetDate (strdate) {var St = strdate; var a = St.split (""); var B = A[0].split ("-" var C = A[1].split (":" var date = new date (b[0], b[1], b[2], c[0], c[1], C[2]) return date;} // Test alert (getDate ("2012-9-20 19:46:18" )); </script>
The effect is as follows
Convert string to JS date format