如何使用 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之类的工具。

本文探讨了Java收藏框架的有效使用。 它强调根据数据结构,性能需求和线程安全选择适当的收集(列表,设置,地图,队列)。 通过高效优化收集用法

掌握了入门级TypeScript教程后,您应该能够在支持TypeScript的IDE中编写自己的代码,并将其编译成JavaScript。本教程将深入探讨TypeScript中各种数据类型。 JavaScript拥有七种数据类型:Null、Undefined、Boolean、Number、String、Symbol(ES6引入)和Object。TypeScript在此基础上定义了更多类型,本教程将详细介绍所有这些类型。 Null数据类型 与JavaScript一样,TypeScript中的null

本教程将介绍如何使用 Chart.js 创建饼图、环形图和气泡图。此前,我们已学习了 Chart.js 的四种图表类型:折线图和条形图(教程二),以及雷达图和极地区域图(教程三)。 创建饼图和环形图 饼图和环形图非常适合展示某个整体被划分为不同部分的比例。例如,可以使用饼图展示野生动物园中雄狮、雌狮和幼狮的百分比,或不同候选人在选举中获得的投票百分比。 饼图仅适用于比较单个参数或数据集。需要注意的是,饼图无法绘制值为零的实体,因为饼图中扇形的角度取决于数据点的数值大小。这意味着任何占比为零的实体
