javascript 字符串格式化成date类型

//将字符串转化为日期型
    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 >= 10 )
    {
    CurrentDate += Month + "-";
    }
    else
    {
    CurrentDate += "0" + Month + "-";
    }
    if (Day >= 10 )
    {
    CurrentDate += Day ;
    }
    else
    {
    CurrentDate += "0" + Day ;
    }
    return CurrentDate;
    }

猜你喜欢

转载自cfj.iteye.com/blog/2298080
今日推荐