1. ホーム
  2. javascript

[解決済み] jsonの結果を日付に変換する [重複]。

2023-02-25 17:35:21

質問

重複の可能性があります。

JSONの日付をフォーマットする方法は?

JavaScriptから$getJSONを呼び出すと、次のような結果が得られます。 JavaScriptでstartプロパティを適切な日付に変換するにはどうすればよいですか?

[ {"id":1,"start":"/Date(1238540400000)/"}, {"id":2,"start":"/Date(1238626800000)/"} ]

ありがとうございます。

どのように解決するのですか?

文字列から数字を取り出し、それを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);

部品は

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