更改显示日期格式

时间:2016-10-07 17:54:47

标签: javascript jquery

我有以下代码,我试图提醒单击图像时选择的日期。我收到警报但问题是日期格式。

以下是我得到的警报。我需要mm / dd / yyy的警报。

如果我使用todatestring()方法,那么为什么我要获得1970年1月1日星期四。

以下是javascript代码

3 个答案:

答案 0 :(得分:0)

尝试此功能返回mm / dd / yyyy

function getmmDdYy(thisDate) {
        return ('0' + (thisDate.getMonth() + 1)).slice(-2) +
            '/' +
            ("0" + (thisDate.getDate())).slice(-2) +
            '/' +
            thisDate.getFullYear();
    }

所以在fnUpdateDate()上,请执行此操作:

function fnUpdateDate(date1,date2){
alert(getmmDdYy(new Date(date1)) +" ss "+ getmmDdYy(new Date(date2)));
}

让我知道这是否有效。

答案 1 :(得分:0)

您可以从日期(日,月,年)检索单个组件,并使用它们构建一个字符串(https://stackoverflow.com/a/30272803)。

如果添加库moment.js,您将能够使用格式字符串,如YYYYMMDDhttps://stackoverflow.com/a/27635223)。

答案 2 :(得分:0)

You can pass your date to this function and return the formate as per your need

function formatDate(date) {
                        var d = new Date(date),
                         month = '' + (d.getMonth() + 1),
                         day = '' + d.getDate(),
                         year = d.getFullYear();
                         if (month.length < 2) month = '0' + month;
                         if (day.length < 2) day = '0' + day;
                         return (month+"/"+day+"/"+year);  
                    } 
相关问题