在JavaScript中获取日期间的相对时间戳差值
您是否在任何网站上见过显示时间戳的通知?它会显示类似“12分钟前”、“2天前”、“10小时前”等信息。这与两个日期或时间之间的时间戳差值有关。
此外,一些应用程序会显示该设备上次登录时间是22小时前。因此,获取两个日期之间的时间戳差值有很多用途。
在本教程中,我们将学习获取两个日期之间相对时间戳差值的几种方法。
使用getTime()方法和日期创建自定义算法
在JavaScript中,我们可以使用new Date()构造函数创建一个日期对象。此外,我们可以将特定日期作为Date()构造函数的参数,以该日期值初始化日期对象。
getTime()方法返回从1970年1月1日到当前日期的总秒数。因此,我们可以找到两个日期的总毫秒数,并将它们相减以获得毫秒差。使用该毫秒数,我们可以找到以秒、分钟、年等为单位的时间戳差值。
语法
用户可以按照以下语法获取两个日期之间的相对时间戳差值。
let second_diff = (current_date.getTime() - previous_date.getTime())/1000;
在上例语法中,current_date和pervious_date是两个不同的日期。我们使用getTime()方法获取两个日期之间的毫秒差。
注意 − 通过将second_diff变量的值与毫秒数进行比较,您可以获得相对时间戳差值。
步骤
用户可以按照以下步骤查找以天、月、年等不同单位表示的两个日期之间的相对时间戳。
步骤1 − 创建两个不同的日期。
步骤2 − 使用getTime()方法获取两个日期的总毫秒数,并计算它们的差值。此外,将毫秒差除以1000将其转换为秒,并将其存储在second_diff变量中。
步骤3 − 现在,使用if-else条件语句查找相对时间戳差值。
步骤4 − 如果second_diff的值小于60,则差值以秒为单位。如果second_diff的值介于60和3600之间,则差值以小时为单位。用户也可以以此类推计算天、月和年。
示例
在下面的示例中,我们使用Date构造函数创建了两个不同的日期对象,并使用了上述步骤来查找两个日期之间的相对时间戳。
在输出中,用户可以观察到以下代码表示以月为单位的时间戳差值。
<html>
<body>
<h3>Getting the relative timestamp difference between two dates using the <i> custom algorithm </i></h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
// creating the current date
let current_date = new Date();
// previous date
let previous_date = new Date("jan 14, 2022 12:21:45");
// finding the difference in total seconds between two dates
let second_diff = (current_date.getTime() - previous_date.getTime()) / 1000;
output.innerHTML += "The first date is " + current_date + "</br>";
output.innerHTML += "The second date is " + previous_date + "</br>";
// showing the relative timestamp.
if (second_diff < 60) {
output.innerHTML += "difference is of " + second_diff + " seconds.";
} else if (second_diff < 3600) {
output.innerHTML += "difference is of " + second_diff / 60 + " minutes.";
} else if (second_diff < 86400) {
output.innerHTML += "difference is of " + second_diff / 3600 + " hours.";
} else if (second_diff < 2620800) {
output.innerHTML += "difference is of " + second_diff / 86400 + " days.";
} else if (second_diff < 31449600) {
output.innerHTML += "difference is of " + second_diff / 2620800 + " months.";
} else {
output.innerHTML += "difference is of " + second_diff / 31449600 + " years.";
}
</script>
</body>
</html>
使用Intl的RelativeTimeFormat() API
Intl指的是国际化API。它包含各种日期和时间格式化方法。我们可以使用Intl对象的RelativeTimeFormat()方法获取两个日期之间的相对时间戳。
语法
用户可以按照以下语法使用RelativeTimeFormat() API获取两个日期之间的相对时间戳。
var relativeTimeStamp =
new Intl.RelativeTimeFormat("en", { numeric: "auto",});
// compare the value of RelativeTimeStamp with milliseconds of different time units
在上例语法中,RelativeTimeFormat()方法返回时间戳差值。time_Stamp_unit是一个对象,包含不同的时间单位及其总毫秒数。
步骤
步骤1 − 创建一个单位对象,其中包含时间单位作为键,以及该时间单位的总毫秒数作为值。
步骤2 − 获取两个日期之间的毫秒时间差。
步骤3 − 现在使用for-in循环遍历time_stamp_unit对象,并检查second_diff的值是否大于特定时间的总毫秒数;使用RelativeTimeFormat() API的format方法以该特定单位格式化时间戳。
步骤4 − 之后,中断for循环。
示例
在下面的示例中,我们使用了RelativeTimeFomrat()方法来获取两个日期之间的相对时间戳差值,如上述语法和步骤中所解释的那样。
<html>
<body>
<h3>Getting the relative timestamp difference between two dates using the <i> RelativeTimeFormat() </i> method </h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
let current_date = new Date();
let previous_date = new Date("jan 14, 2022 12:21:45");
// finding the difference in total seconds between two dates
let second_diff = current_date.getTime() - previous_date.getTime();
output.innerHTML += "The first date is " + current_date + "</br>";
output.innerHTML += "The second date is " + previous_date + "</br>";
var time_Stamp_unit = {
year: 31536000000,
month: 31536000000 / 12,
day: 86400000,
hour: 3600000,
minute: 60000,
second: 1000,
};
var relativeTimeStamp = new Intl.RelativeTimeFormat("en", {
numeric: "auto",
});
// iterate through all time stamps
for (var ele in time_Stamp_unit) {
// if second_diff's value is greater than particular timesapm unit's total millisecond value, format accordingly
if (Math.abs(second_diff) > time_Stamp_unit[ele] || ele == "second") {
output.innerHTML += "The difference between two dates is " + relativeTimeStamp.format(
Math.round(second_diff / time_Stamp_unit[ele]), ele
);
break;
}
}
</script>
</body>
</html>
用户学习了如何使用if-else语句和RelativeTimeFormat() API的format()方法查找两个日期之间的相对时间戳。用户可以根据自己的需求使用这两种方法。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP