---
title: "javascript 字符串格式化成date类型"
author: "cfj"
date: "2016-05-13T19:45:15Z"
tags: []
categories: ["Web前端"]
slug: "javascript-date"
draft: false
lastmod: "2016-05-13T19:45:15Z"
id: 2298080
url: "https://cfj.iteye.com/blog/2298080"
---

//将字符串转化为日期型  
    function getStrToDate(strDate) {  
        var date = eval('new Date(' + strDate.replace(/\\d+(?=-\[^-]+$)/,  
         function (a) { return parseInt(a, 10) - 1; }).match(/\\d+/g) + ')');  
        return date;  
    }

格式化成2016-01-05

//格式化日期  
    function getNowFormatDate(crDate){  
    var Year = 0;  
    var Month = 0;  
    var Day = 0;  
    var CurrentDate = "";  
    Year= crDate.getFullYear();//ie火狐下都可以  
    Month= crDate.getMonth()+1;  
    Day = crDate.getDate();  
    CurrentDate += Year + "-";  
    if (Month &gt;= 10 )  
    {  
    CurrentDate += Month + "-";  
    }  
    else  
    {  
    CurrentDate += "0" + Month + "-";  
    }  
    if (Day &gt;= 10 )  
    {  
    CurrentDate += Day ;  
    }  
    else  
    {  
    CurrentDate += "0" + Day ;  
    }  
    return CurrentDate;  
    }