2014-06-11 73 views
0

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

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 
+0

让你有' timestamp' /'长日期格式作为输入? – sabithpocker

+1

看起来你正在处理odata返回日期..如果是这样的话,那么节省一些时间,并在你的项目中加入moment.js。它了解这些日期并提供了出色的格式化功能。 – Stephen

回答

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

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);