Convert string to JS date format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.