简体   繁体   中英

How to parse datetime returned in json from webapi to print at front end in yyyy/mm/dd format using ajax

[
    {"AIRLINE":"Air India","FLIGHT_NO_":"AI-202","DATE":"2019-11-23T00:00:00","DEPARTURE":"08:15:00","ARRIVAL":"11:45:00","FROM":"BOM","TO":"KOL","PRICE":2800.0000},
    {"AIRLINE":"Vistara","FLIGHT_NO_":"VS-105","DATE":"2019-11-23T00:00:00","DEPARTURE":"10:00:00","ARRIVAL":"12:30:00","FROM":"BOM","TO":"KOL","PRICE":3100.0000},
    {"AIRLINE":"Jet Airways","FLIGHT_NO_":"JA-303","DATE":"2019-11-23T00:00:00","DEPARTURE":"10:20:00","ARRIVAL":"22:15:00","FROM":"BOM","TO":"KOL","PRICE":4000.0000}
]

I have above json and i am doing this in my script.

                             var rows = "<tr>" +
                                 "<td id='AIRLINE'>" + item.AIRLINE + "</td>" +
                                 "<td id='FLIGHT'>" + item.FLIGHT_NO_ + "</td>" +
                                 "<td id='DATE'>" + Date(item.DATE) + "</td>" +
                                 "<td id='DEPARTURE'>" + item.DEPARTURE + "</td>" +
                                 "<td id='ARRIVAL'>" + item.ARRIVAL +
                                 "<td id='FROM'>" + item.FROM +
                                 "<td id='TO'>" + item.TO +
                                 "<td id='PRICE'>" + item.PRICE +
                                 "</td>" +
                                 "</tr>";
                            $('#tblBody').append(rows);

but it is returning current system datetime.

Vistara VS-105 Tue Nov 19 2019 12:42:06 GMT+0530 (India Standard Time) 10:00:00 12:30:00 BOM KOL 3100
Jet Airways JA-303 Tue Nov 19 2019 12:42:06 GMT+0530 (India Standard Time) 10:20:00 22:15:00 BOM KOL 4000

The function Date() returns the current system date. You just need to change Date() to new Date() to create a new instance of date object. Check the difference in the snippet below:

 document.write (Date("2019-11-23T00:00:00") + "<br>"); document.write (new Date("2019-11-23T00:00:00"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM