如何使用 JavaScript 將 JSON 結果轉換為日期?
JSON 是一種功能強大的資料格式,用於在伺服器和用戶端之間交換資料。很多時候,JSON 資料是以字串格式接收的,我們需要將其轉換為可用的 JSON 物件。在這個過程中,一個重要的需求是將字串資料轉換為Date格式。在本文中,我們將學習如何使用 Javascript 將 JSON 結果轉換為日期字串。
JSON 物件包含這樣的日期 -
{ name: "John", time: '/Date(1559072200000)/' }
結果將會是 -
Wed May 29 2019 01:06:40 GMT+0530 (India Standard Time)
這裡有一些實現這一目標的方法 -
使用 string.replace 方法
使用正規表示式
#方法一:使用String的replace()方法
JavaScript中的replace方法用於將一個字串的一部分替換為另一個字串。以下是使用 String.replace 方法將 JSON 結果轉換為日期的步驟。
將字串「/Date(」的第一部分替換為空字串
將字串“)/”的最後部分替換為空字串
#透過解析 JSON 字串中的毫秒數來建立新的 Date 物件
#現在您獲得了日期,您可以將其用作普通的 javascript 日期。
範例
在此範例中,我們使用 String.replace() 方法將 JSON 結果轉換為日期。
<html> <body> <h2>Convert JSON results into a date using JavaScript</h2> <p>Click the following button to convert JSON results into a date</p> <button id="btn" onclick="convert( )"> Click Here </button> <br> <h3>Input Data : </h3> <p id="input"> /Date(1559072200000)/ </p> <h3> Resulting Date: </h3> <p id="output"> </p> <script> function convert() { // Store the JSON date string in a variable var jsonDate = '/Date(1559072200000)/'; // Replace the first part of the string "/Date(" with an empty string jsonDate = jsonDate.replace("/Date(", " ") // Replace the last part of the string ")/" with an empty string jsonDate = jsonDate.replace(")/", " ") // Create a new Date object by parsing the number of milliseconds from the JSON string let strDate = new Date(parseInt(jsonDate)); // Get the and output element in the HTML document let output = document.getElementById("output") // Set the inner text of the output element to the formatted date output.innerText = strDate; } </script> </body> </html>
方法 2:使用正規表示式
以下是使用正規表示式將 JSON 結果轉換為日期的步驟。
使用正規表示式從 JSON 日期字串中提取 unix 時間戳記
#透過解析 JSON 字串中的毫秒數來建立新的 Date 物件
#現在您獲得了日期,您可以將其用作普通的 JavaScript 日期。
<html> <body> <h2>Convert JSON results into a date using JavaScript</h2> <p>Click the following button to convert JSON results into a date</p> <button id="btn" onclick="convert( )"> Click Here </button> <br> <h3>Input Data : </h3> <p id="input"> /Date(1559072200000)/ </p> <h3> Resulting Date: </h3> <p id="output"> </p> <script> // Function to convert the JSON date format to a readable date function convert() { // The JSON date string in the format '/Date(unixTimestamp)/' var jsonDate = '/Date(1559072200000)/'; // Extract the Unix timestamp from the JSON date string using regex jsonDate = jsonDate.match(/\d+/); // Create a new Date object using the unix timestamp let strDate = new Date(parseInt(jsonDate)); // Get a reference to the HTML element with the id "output" let output = document.getElementById("output"); output.innerText = strDate; } </script> </body> </html>
以上是如何使用 JavaScript 將 JSON 結果轉換為日期?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

本教程將介紹如何使用 Chart.js 創建餅圖、環形圖和氣泡圖。此前,我們已學習了 Chart.js 的四種圖表類型:折線圖和條形圖(教程二),以及雷達圖和極地區域圖(教程三)。 創建餅圖和環形圖 餅圖和環形圖非常適合展示某個整體被劃分為不同部分的比例。例如,可以使用餅圖展示野生動物園中雄獅、雌獅和幼獅的百分比,或不同候選人在選舉中獲得的投票百分比。 餅圖僅適用於比較單個參數或數據集。需要注意的是,餅圖無法繪製值為零的實體,因為餅圖中扇形的角度取決於數據點的數值大小。這意味著任何占比為零的實體

掌握了入門級TypeScript教程後,您應該能夠在支持TypeScript的IDE中編寫自己的代碼,並將其編譯成JavaScript。本教程將深入探討TypeScript中各種數據類型。 JavaScript擁有七種數據類型:Null、Undefined、Boolean、Number、String、Symbol(ES6引入)和Object。 TypeScript在此基礎上定義了更多類型,本教程將詳細介紹所有這些類型。 Null數據類型 與JavaScript一樣,TypeScript中的null
