在 JavaScript 中将毫秒转换为日期
本文将讨论使用 JavaScript 的默认日期对象和方法从毫秒生成日期。
在 JavaScript 中将毫秒转换为日期
要在 JavaScript 中从毫秒生成日期,我们可以使用默认的 JavaScript 日期对象及其方法。 首先,我们必须要求将毫秒值作为参数传递给日期对象,并将日期转换为所需的日期格式。
有多种转换日期格式的默认方法。 大多数情况下,开发人员需要将日期内容转换为字符串,语法如下所示。
基本语法:
let date = new Date(milliseconds); // convert date from milliseconds
date.toString(); // change format
在 JavaScript 中从毫秒生成字符串日期
下面的示例将创建一个简单的网页,我们将在其中生成当前日期对象并通过 date.time() 方法将其转换为毫秒。 然后,使用该毫秒值,我们将在单击按钮时生成当前日期的字符串格式。
示例代码:
<!DOCTYPE html>
<html>
<body>
<h2>DelftStack learning</h2>
<h3>JavaScript convert milliseconds to date example</h3>
<p id="millisecondsDate" > </p>
<button onClick="myFunction()">
click here
</button>
<p id="convertedDate"> </p>
<script>
var original = document.getElementById('millisecondsDate');
var result = document.getElementById('convertedDate');
var time = new Date().getTime(); // current date in milliseconds
original.innerHTML = "Milliseconds = " + time;
function myFunction() {
var date = new Date(time);
result.innerHTML = date.toString(); // final date in string
}
</script>
</body>
</html>
在上面的 HTML 源代码中,我们使用了段落标记 <p></p>
来显示转换后的日期。 在 <script>
标签中,我们生成了当前日期并获取了毫秒数。
我们声明了 myFunction()
并通过将毫秒作为参数传递来生成一个新日期。 现在,使用 toString()
方法将日期转换为字符串格式。
我们使用 innerHTML 将最终转换的日期分配给段落标签。 在这里,我们通过单击按钮调用了 myFunction()
。
可以将上面的源码保存为HTML扩展名,在浏览器中打开就可以看到效果了。
JavaScript 中毫秒级的常见日期转换
下面介绍JavaScript中将毫秒数转换为各种日期格式的常用方法。
let originalDate = new Date(milliseconds);
originalDate.toLocaleString(); // output will be: "D/MM/YYYY, H:MM:SS PM/AM"
originalDate.toLocaleDateString(); //output will be: "D/MM/YYYY"
originalDate.toDateString(); // output will be: "Day Month DD YYYY"
originalDate.toTimeString(); // output will be: "HH:MM:SS GMT+0530"
originalDate.toString(); // output will be: "Day Month DD YYYY HH:MM:SS GMT+0500"
originalDate.toLocaleTimeString(); // output will be: "H:MM:SS AM/PM"
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。