Kate
Kate

Reputation: 165

JS not parsing Date correctly on production server

I get my data from the db using ajax, I'm using those 2 lines of code to display the date in an input Date :

var dateReceptionDE = new Date(tache.dateReceptionDE); 
console.log("From DB : " + tache.dateReceptionDE);
console.log("In the variable: " + dateReceptionDE);
document.getElementById("ReceiveEDDateEdit").valueAsDate = new 
           Date(dateReceptionDE.getFullYear(),dateReceptionDE.getMonth(),dateReceptionDE.getDate(),12);

This is the result in the localhost :
Result on console

But when I deploy it in the production server, I get this:
enter image description here

enter image description here

PS 1: I'm working on a project using MySql (as a DB), JEE (as Back-end) and JS.

PS 2: Date type in DB is DATETIME, I tried DATE but still the same result.

PS 3: Tomcat version is 8.5.43 (Both local and prod server)

UPDATE : I forget to mention that I'm using also json between java servlet and js.

Object task= tacheDao.selectREQUEST();

        String json = new Gson().toJson(task);

        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.write(json);

AJAX :

var task= JSON.parse(data); 

Thanks in advance,

Upvotes: 0

Views: 288

Answers (2)

Kate
Kate

Reputation: 165

I solve this problem by modifying GSON Format Date in JSON object :

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
String json =  gson.toJson(task);

Upvotes: 1

Jude Maranga
Jude Maranga

Reputation: 886

The reason why that is not working in live is because your MySQL query is returning a date string that can't be parsed by JS immediately. In your SQL query, try formatting the date column, something like this:

SELECT DATE_FORMAT(dateColumnName, "%m/%d/%Y") AS dateReceptionDE FROM Table_Name;

Upvotes: 0

Related Questions