JavaScript 获取日期之间的相对时间戳差异
您是否曾经看到过任何网站上显示时间戳的通知?它显示类似于“12分钟前”,“2天前”,“10小时前”等内容。这与两个日期或时间之间的时间戳差异相关。
此外,一些应用程序显示该设备的最后登录是22小时前。因此,有许多用途可以获取两个日期之间的时间戳差异。
在本教程中,我们将学习不同的方法来获取两个日期之间的相对时间戳差异。
使用 date 的 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()方法来查找两个日期之间的相对时间戳。用户可以根据自己的需求同时使用这两种方法。