将日期转换为不同格式的jquery / javascript

时间:2013-03-18 07:28:58

标签: javascript jquery

function loadTable() { //function 
    var reportedFromDate = document.IssueForm.reportedFromDate.value; //getting value from input field
    alert(reportedFromDate); //displays dd-mm-yy 31-02-13
}

我想将其转换为 31-FEB-13

2 个答案:

答案 0 :(得分:1)

检查以下代码段:

  function loadTable() { //function 
        var reportedFromDate = document.IssueForm.reportedFromDate.value; 
        alert(reportedFromDate);  // your format
        var parts=reportedFromDate.split("-");
        var date=parts[0];
        var month=parts[1];
        var year=parts[2];

        var mon=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEPT","OCT","NOV","DEC"];
        var m=parseInt(month);
        alert( date +"-"+mon[m-1]+"-"+year); // required format
    }

答案 1 :(得分:0)

您需要执行以下步骤:

1. Create one array of months like 
var months = ["JAN","FEB"....,"DEC"];
2. Split the date with (-) seperator
3. Take the 1st index (not 0) and search through the Array (months)
4. Display the formatted date.