-1

EDIT:

The server was sending a Date Object even if the browser was displaying the Date Object as a normal string. Once I found that I just changed the type of the data from Object to String, and the date remained as expected. Thanks for your help.

=========================================================================

Working on a webapp to display some data I retrieve from an API on a raspberry. The data is correctly sent over by the API through a MySQL request to the API server but the datetime string is changed when the data arrives to the nodeJS application server, like in the picture:

datetime data

As you can see from the image, on the right is the data sent from the mysql server to the nodejs server. I am console.logging the data directly after the request is sent from the API endpoint to the mysql server. On the left is the same data displayed in JSON in the browser by sending a normal GET request to the same API endpoint that is logged in the terminal window on the right. You should notice that the data is the same, but on the left side the dateandtime string has been changed by one hour for no reason that I can think of. I do not process the dateandtime data on the servers so it's not been changed by some function or process that I created.

Thanks in advance for the help!

1 Answers1

1

The two dates are technically equivalent, just in different display formats. If you want to the console to print the date in ISO 8601 format (as on the left) then you must be explicit about that. You can do something like this:

results.each(r=>r.dateandtimeisostring=r.dateandtime.toISOString())
console.log(results). 

dateandtimesisotring will be in the same format as the browser.

Note though, that dateandtimesisotring is a String and not a Date. For any computations you should use dateandtime.

Sello Mkantjwa
  • 1,798
  • 1
  • 20
  • 36
  • but why does it change the dateandtime string without me telling the server to do so? – 404answernotfound Oct 30 '17 at 10:35
  • 1
    @mnemosdev it did not change it. Internally they are all the same. Its way it is printed out that changes. Take a look at this: https://stackoverflow.com/a/36216072/2212208. There is no restriction on how a `Date` should be printed so your browser and node have decided to do it in different ways. Like I said, the 2 are equivalent, but just displayed differently. – Sello Mkantjwa Oct 30 '17 at 11:26
  • so ill have to see how the final "physical" server will display the date and in that case I should create a function to "move" the time one hour ahead T.T – 404answernotfound Oct 30 '17 at 13:15