日期转换为jquery

时间:2014-06-11 07:38:47

标签: javascript jquery

我需要给定日期的DD-MM-YYYY格式。

var todaydate = /Date(1394908200000)/; //Serialize date
var date = eval("new " + todaydate.replace(/\//g, ""));
alert('date is :'+date)

但输出看起来像,

date is :Wed Jun 11 2014 00:00:00 GMT+0530 (India Standard Time)

预期输出,如

date is :11-06-2014

3 个答案:

答案 0 :(得分:5)

试试这个

var date = new Date(); //new Date(1394908200000)
function convert(str) {
    var date = new Date(str),
        mnth = ("0" + (date.getMonth() + 1)).slice(-2),
        day = ("0" + date.getDate()).slice(-2);
    return [day, mnth, date.getFullYear()].join("-");
}

var final = convert(date);
alert('date is :' + final)

DEMO

答案 1 :(得分:2)

除了像其他人提到的JS之外,您还可以使用jquery ui plug-in中的.datepicker

var dt = $.datepicker.formatDate('dd-mm-yy', new Date(1394908200000));

alert(dt);

JSFiddle, use jquery ui plug-in

答案 2 :(得分:1)

WORKING FIDDLE

试试这个 -

var date = new Date();

function myDateFormatter (dateobject) {
    var d = new Date(dateobject);
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    if (day < 10) {
        day = "0" + day;
    }
    if (month < 10) {
        month = "0" + month;
    }
    var date = day + "-" + month + "-" + year;

    return date;
}; 
var dateformat = myDateFormatter(date);
alert('date is :' + dateformat);