Home > Mobile >  How to modify date formate in ajax js code?
How to modify date formate in ajax js code?

Time:05-29

I have a js ajax code:

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

The issue is the time format is like:

2021-08-05T22:10:55.255Z

How to modify this date formate to something like:

2021-08-05 22:10

CodePudding user response:

You should be able to just format it within that success function :

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

CodePudding user response:

Check out the docs for Moment.js, this should do the trick for you: http://momentjs.com/docs/#/parsing/string format.

example: <span class='time'>' moment(data.fields.time).format("YYYY-MM-D HH:mm") '</span>'

  • Related