首页 > web前端 > js教程 > 正文

如何使用 JavaScript 将 JSON 结果转换为日期?

王林
发布: 2023-09-08 11:33:08
转载
742人浏览过

如何使用 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) 
登录后复制

这里有一些实现这一目标的方法 -

立即学习Java免费学习笔记(深入)”;

  • 使用 string.replace 方法

  • 使用正则表达式

方法一:使用String的replace()方法

JavaScript中的replace方法用于将一个字符串的一部分替换为另一个字符串。以下是使用 String.replace 方法将 JSON 结果转换为日期的步骤。

  • 将字符串“/Date(”的第一部分替换为空字符串

    Find JSON Path Online
    Find JSON Path Online

    Easily find JSON paths within JSON objects using our intuitive Json Path Finder

    Find JSON Path Online 30
    查看详情 Find JSON Path Online
  • 将字符串“)/”的最后部分替换为空字符串

  • 通过解析 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中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号