如何使用 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 结果转换为日期?的详细内容,更多请关注双恒网络其它相关文章!

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!

云资源网 » 如何使用 JavaScript 将 JSON 结果转换为日期?

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
提示下载完但解压或打开不了?
最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们.。
你们有qq群吗怎么加入?
当然有的,如果你是帝国cms、易优cms、和pbootcms系统的爱好者你可以加入我们的QQ千人交流群https://www.sudo1.com/page-qun.html。
  • 会员数(个)
  • 12310资源数(个)
  •        
  • 资源(G)
  •        
  • 今日下载
  • 1505稳定运行(天)

提供最优质的资源集合

立即查看 了解详情