Javascript 数组-转换所有日期字段的日期格式

Javascript 数组-转换所有日期字段的日期格式,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我有以下数组,其中有一些字段,如dateofbirth,我需要在其中删除时间并将格式更改为MM-DD-YYYY var records = [ { "recordno":"000001", "firstname":"Bo", "middlename":"G", "lastname":"Dallas", "gender":"male", "dateofbirth":"2014-05-31T18:

我有以下数组,其中有一些字段,如
dateofbirth
,我需要在其中删除时间并将格式更改为
MM-DD-YYYY

var records = [
    {
        "recordno":"000001",
        "firstname":"Bo",
        "middlename":"G",
        "lastname":"Dallas",
        "gender":"male",
        "dateofbirth":"2014-05-31T18:30:00.000Z",
        "dateofdeath":null,
        "_id":"538c701c84ee56601f000063",
    },
    {
        "recordno":"000001",
        "firstname":"Bo",
        "middlename":"G",
        "lastname":"Dallas",
        "gender":"male",
        "dateofbirth":"2014-05-31T18:30:00.000Z",
        "dateofdeath":null,
        "_id":"538c701c84ee56601f000067",
    },
];

如何转换数组中所有数据类型为
date
的字段的日期格式?

是数组中的日期对象吗?是否要将它们转换为字符串?那么也许这会奏效

for (var i = 0; i < records.length; ++i) {
    var birthDate = new Date(records[i].dateofbirth);

    var newBirthDateString = ('0' + birthDate.getDate()).slice(-2) + '-'
                + ('0' + (birthDate.getMonth()+1)).slice(-2) + '-'
                + birthDate.getFullYear();
    records[i].dateofbirth = newBirthDateString;

    if (records[i].dateofdeath !== null) {
        var deathDate = new Date(records[i].dateofdeath);

        var newDeathDateString = ('0' + deathDate.getDate()).slice(-2) + '-'
                + ('0' + (deathDate.getMonth()+1)).slice(-2) + '-'
                + deathDate.getFullYear();
        records[i].dateofdeath = newDeathDateString;
    }    
}
for(变量i=0;i
请检查以下代码。希望这对你有帮助

var records = [
    {
        "recordno":"RF-000001",
        "firstname":"Bo",
        "middlename":"G",
        "lastname":"Dallas",
        "gender":"male",
        "dateofbirth":"2014-05-31T18:30:00.000Z",
        "dateofdeath":null,
        "_id":"538c701c84ee56601f000063",
    },
    {
        "recordno":"RF-000001",
        "firstname":"Bo",
        "middlename":"G",
        "lastname":"Dallas",
        "gender":"male",
        "dateofbirth":"2014-05-31T18:30:00.000Z",
        "dateofdeath":null,
        "_id":"538c701c84ee56601f000067",
    },
];

for(var i=0;i<records.length;i++){
    if(records[i].dateofbirth){
        var splitDateTime = records[i].dateofbirth.split("T");
        var splitDate = splitDateTime[0].split("-");
        records[i].dateofbirth = splitDate[1] +"-"+ splitDate[2] +"-"+ splitDate[0];
    }
} 
for(var i=0;i<records.length;i++){
    for( var attrName in records[i]){
        if(records[i][attrName].indexOf("T") == 10){
             var splitDateTime = records[i][attrName].split("T");
             var splitDate = splitDateTime[0].split("-");
             records[i][attrName] = splitDate[1] +"-"+ splitDate[2] +"-"+ splitDate[0];
        }

    }
}   
var记录=[
{
“记录编号”:“RF-000001”,
“名字”:“博”,
“中间名”:“G”,
“姓氏”:“达拉斯”,
“性别”:“男性”,
“出生日期”:“2014-05-31T18:30:00.000Z”,
“死亡日期”:空,
“_id”:“538c701c84ee56601f000063”,
},
{
“记录编号”:“RF-000001”,
“名字”:“博”,
“中间名”:“G”,
“姓氏”:“达拉斯”,
“性别”:“男性”,
“出生日期”:“2014-05-31T18:30:00.000Z”,
“死亡日期”:空,
“_id”:“538c701c84ee56601f000067”,
},
];

对于(var i=0;i当我在我的.js中使用此代码时,它会给我语法错误。我尝试过它,它会格式化dateofbirth,但不会格式化dateofdeath。编辑为包含dateofdeath。这是可行的,但我的数组中还有两个日期字段。有没有快捷方式。。