Tengo un código js ajax:
success:function(res){ var _html=''; var json_data=$.parseJSON(res.posts); $.each(json_data,function (index,data) { _html+='<span class='time'>'+data.fields.time+'</span>'; }); $(".post-wrapper").append(_html); }El problema es que el formato de hora es como:
2021-08-05T22:10:55.255ZCómo modificar este formato de fecha a algo como:
2021-08-05 22:10Consulte los documentos de Moment.js, esto debería funcionar para usted: http://momentjs.com/docs/#/parsing/string+format .
ejemplo: <span class='time'>'+moment(data.fields.time).format("YYYY-MM-D HH:mm")+'</span>'
Debería poder formatearlo dentro de esa función de éxito:
var _html=''; var json_data=$.parseJSON(res.posts); $.each(json_data,function (index,data) { let datetime = data.fields.time; let formatted_date = datetime.getFullYear() + "-" + (datetime.getMonth() + 1) + "-" + datetime.getDate() + " " + datetime.getHours() + ":" + datetime.getMinutes(); _html+='<span class='time'>'+ formatted_date +'</span>'; });