[英]Calculate difference between two timestamps
我正在使用抽搐 api 和 TMI.JS
我正在嘗試獲取followed_at
的時間戳。 我得到的時間戳是2021-12-25T15:49:57Z
,我將如何 go 獲取當前時間戳,然后計算差異。 因此,在這種情況下,它會返回followername has been following streamername for 20 days 19 hours
(在撰寫本文時,距離followed_at
時間戳已有 20 天 19 小時。)
if (message.toLowerCase() === '!followage') {
client.say(channel, `@${tags.username}, does not work yet `)
console.log(`${channel.id}`)
console.log(`${tags['user-id']}`)
async function getData() {
const res = await fetch(`https://api.twitch.tv/helix/users/follows?to_id=226395001&from_id=${tags['user-id']}`, { method: 'get', headers: {
'Client-Id': 'cut out for a reason',
'Authorization': 'Bearer cut out for a reason'
}});
const data = await res.json();
console.log(data)
if (data.data[0]) {
client.say(channel, `@${tags.username}, followed at ${data.data[0].followed_at}`)
} else {
client.say(channel, `You aren't followed!`)
}
}
getData()
}
以上是我獲取它,然后將其發送到頻道的代碼。
以下是您的操作方法:
const followedAt = new Date('2021-12-25T15:49:57Z');
const currentDate = new Date();
const diffTime = Math.abs(currentDate - followedAt);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffTime + " milliseconds");
console.log(diffDays + " days");
這就是獲得天數和小時數的方法:
const followedAt = new Date('2021-12-25T15:49:57Z');
const currentDate = new Date();
const diffTime = Math.abs(currentDate - followedAt);
const diffTotalHours = Math.floor(diffTime / (1000 * 60 * 60));
const diffDays = Math.floor(diffTotalHours / 24);
const diffHoursWithoutDays = diffTotalHours % 24;
console.log(`${diffDays} days and ${diffHoursWithoutDays} hours`);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.