首頁 > web前端 > js教程 > 主體

如何使用 JavaScript 將 JSON 結果轉換為日期?

王林
發布: 2023-09-08 11:33:08
轉載
575 人瀏覽過

如何使用 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中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!