Converting JSON Results to a Date

Converting json results to a date

You need to extract the number from the string, and pass it into the Date constructor:

var x = [{
"id": 1,
"start": "\/Date(1238540400000)\/"
}, {
"id": 2,
"start": "\/Date(1238626800000)\/"
}];

var myDate = new Date(x[0].start.match(/\d+/)[0] * 1);

The parts are:

x[0].start                                - get the string from the JSON
x[0].start.match(/\d+/)[0] - extract the numeric part
x[0].start.match(/\d+/)[0] * 1 - convert it to a numeric type
new Date(x[0].start.match(/\d+/)[0] * 1)) - Create a date object

Convert JSON /Date(1404860400000)/ to javascript date

Solved it myself using JQuery moment:

Created a method to run each JSON date through:

function parseJsonDate(jsonDateString) {
return moment(jsonDateString).format("D-MMM-YY").toUpperCase();
}

And called it when rendering the table:

"<td> " + parseJsonDate(auditList[s].Date) + "</td>" +

How to parse JSON to receive a Date object in JavaScript?

There is no standard JSON representation of dates. You should do what @jAndy suggested and not serialize a DateTime at all; just send an RFC 1123 date string ToString("r") or a seconds-from-Unix-epoch number, or something else that you can use in the JavaScript to construct a Date.

Convert Json date to Date in javascript

You could use a function which return a date if the date string is in the wanted form or the value itself.

function getDateIfDate(d) {    var m = d.match(/\/Date\((\d+)\)\//);    return m ? (new Date(+m[1])).toLocaleDateString('en-US', {month: '2-digit', day: '2-digit', year: 'numeric'}) : d;}
console.log(getDateIfDate("/Date(1460008501597)/"));console.log(getDateIfDate('abc'));

Angular 8 way to convert json response with string dates to JS date?

The short answer is no as JSON format does not allow date types. See this: https://www.w3schools.com/JS/js_json_datatypes.asp

I don't think you have any choice other than to transform them into Date objects, and besides, it's relatively trivial given your string date format:

ndate = new Date("1986-05-04T22:59:59.000Z")

How do I format a Microsoft JSON date?

eval() is not necessary. This will work fine:

var date = new Date(parseInt(jsonDate.substr(6)));

The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor.


I have intentionally left out the radix (the 2nd argument to parseInt); see my comment below.

Also, I completely agree with Rory's comment: ISO-8601 dates are preferred over this old format - so this format generally shouldn't be used for new development.

For ISO-8601 formatted JSON dates, just pass the string into the Date constructor:

var date = new Date(jsonDate); //no ugly parsing needed; full timezone support

What is the right JSON date format?

JSON itself does not specify how dates should be represented, but JavaScript does.

You should use the format emitted by Date's toJSON method:

2012-04-23T18:25:43.511Z

Here's why:

  1. It's human readable but also succinct

  2. It sorts correctly

  3. It includes fractional seconds, which can help re-establish chronology

  4. It conforms to ISO 8601

  5. ISO 8601 has been well-established internationally for more than a decade

  6. ISO 8601 is endorsed by W3C, RFC3339, and XKCD

That being said, every date library ever written can understand "milliseconds since 1970". So for easy portability, ThiefMaster is right.

Convert JSON date list to python date format

You can use datetime.fromtimestamp() in datetime module to convert epochtime to datetime, as follows:

from datetime import datetime

birthdate_json = [
'/Date(1013230800000)/',
'/Date(1016600400000)/',
'/Date(1010466000000)/',
'/Date(1017205200000)/',
'/Date(1020052800000)/'
]
birthdate = []
for i in range(len(birthdate_json)):
epoch_time = int(birthdate_json[i][6:-2])/1000
datetime_type_value = datetime.fromtimestamp(epoch_time)
# uncomment next line If you want str value of datetime such as ["2022-02-23", "2022-02-24" ...]
# datetime_type_value = datetime_type_value.strftime("%F")
birthdate.append(datetime_type_value)

print(birthdate)

# datetime type values will be printed:
# [datetime.datetime(2002, 2, 9, 14, 0), datetime.datetime(2002, 3, 20, 14, 0), datetime.datetime(2002, 1, 8, 14, 0), datetime.datetime(2002, 3, 27, 14, 0), datetime.datetime(2002, 4, 29, 13, 0)]


Related Topics



Leave a reply



Submit