字串轉換成js的日期格式

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   使用   sp   java   

js字串轉日期格式 ,JavaScript字串轉日期格式 大家都知道JS是根據結果來確定資料類型的。 當然我們也是可以轉化的,下面我就介紹兩種關於JS字串類型轉換成日期類型的方法, 我個人比較喜歡的是第一種方法。 大家有什麼別的好方法也可以分享一下。 1.eval方法轉換方法,---推薦使用這種方法 我寫成了一個方法大家要吧直接調用

?
<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.第二種方法 是使用拆分數組的方式。不建議這樣使用,因為這樣日期格式不靈活 方法如下

<script type="text/javascript">        //字串轉日期格式,strDate要轉為日期格式的字串        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; } //測試 alert(getDate("2012-9-20 19:46:18")); </script>

效果如下

字串轉換成js的日期格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.