Asked by:
JavaScript Date input from Database unusual Unix String /Date(timestamp)/

Question
-
User-544325736 posted
Hello everyone
For some reason I am bringing in a datetime from my db to JavaScript and im getting it as ‘/Date(1558674000000)/’ What can I do to transfer it back to a normal readable date?
I was looking for maybe a built in method but so far I tried a bunch of them and haven’t had any luck yet. Otherwise maybe I need to use a substring and convert it with a function I can make timestamp to readable date. I really don’t know why it is adding /Date( if it was just a timestamp I could of easily done it.
Thank you
Tuesday, June 25, 2019 1:47 PM
All replies
-
User-544325736 posted
maybe do this?
unix_timestamp = jsonDate.replace('/Date(', '').replace(')/', '');
OR
var date = new Date(parseInt(jsonDate.substr(6)));
let me know if there is a better way or what you think of with this.
Thanks
Tuesday, June 25, 2019 2:18 PM -
User753101303 posted
Hi,
AFAIK this is js notation intended to be used with eval (you just need to remove the / characters).
How do you produce this value? You want to inject a date value into JavaScript code or this is a web service? (I look much less often to actual data now).
Tuesday, June 25, 2019 2:38 PM -
User-1333481028 posted
For some reason I am bringing in a datetime from my db to JavaScript and im getting it as ‘/Date(1558674000000)/’ What can I do to transfer it back to a normal readable date?
<script>
var myday = new Date(1558674000000);
document.writeln(myday);</script>
OutPut:
Fri May 24 2019 01:00:00 GMT-0400 (Eastern Daylight Time)
Tuesday, June 25, 2019 2:49 PM -
User-474980206 posted
this is an odd format invented by the mvc team to serialize a date to json. its the javascript syntax for creating a date surrounded by /'s. Oddly its only supported for object to json, its not supported for json to obj.
if you see this format, you are using the old script serializers, not the newer json serializers. while you could do an eval, its more common to use a regex to extract the milliseconds. if you are using moment.js, then its has support.
function MsJsonDateToDate(s) { return new Date(Number(/\d+/.exec(s)[0])) } // or in modern javascript: const MsJsonDateToDate = s => new Date(Number(/\d+/.exec(s)[0]));
Tuesday, June 25, 2019 2:56 PM -
User839733648 posted
Hi ExceedingLife,
According to your description, I suggest that you could use following code to convert a Unix timestamp to usual time in JavaScript.
<script> // Create a new JavaScript Date object based on the timestamp // multiplied by 1000 so that the argument is in milliseconds, not seconds. var unix_timestamp = "1558674000000"; var date = new Date(unix_timestamp * 1000); var hours = date.getHours(); var minutes = "0" + date.getMinutes(); var seconds = "0" + date.getSeconds(); // Display time in 00:00:00 format var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2); console.log(formattedTime); </script>
result:
For more, you could refer to the MDN documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Best Regards,
Jenifer
Wednesday, June 26, 2019 8:01 AM