如何根据时间戳比率获取未来日期

时间:2021-07-24 19:26:08

标签: javascript typescript date-fns

我正在尝试为游戏创建一个虚拟日历。 我的事件会在现实中持续一段时间(1 周、1 个月)。 在游戏中,这些事件应始终等于 1 年。

为了简单起见,我的目标是使用 date-fns 并使用时间戳来保持比例。

假设我有一个活动持续 1 周(现实生活) 在游戏中,这将是 1 年。 如果我尝试在活动开始 + 3 天(几乎一半的活动已过去)时获取游戏中的日期。从理论上讲,我应该在虚拟日历中接近 6 个月,但是在多年后测试它时给了我一个答案。

import {
    addDays,
    addYears,
    getTime
} from "date-fns";

// Initiate Dates
const now = new Date()
const nextWeek = addDays(now, 7);
const nextYear = addYears(now, 1);
// Initiate Timestamp convertions
const currentTimestamp = getTime(now)
const tmrTimestamp = getTime(addDays(now, 3))
const nextWeekTimestamp = getTime(nextWeek)
// Calculate differences
const differenceReal = nextWeekTimestamp - currentTimestamp
const differenceVirtual = getTime(nextYear) - currentTimestamp
console.log(`difference_real : ${differenceReal}`)
console.log(`difference_virtual : ${differenceVirtual}`)
// Calculate the ratio
const ratio = differenceReal / differenceVirtual
// Log information
console.log(`ratio: ${ratio}`)
console.log(`ts_now ${getTime(now)}`)
console.log(`ts_tmr ${getTime(tmrTimestamp)}`)
//Calculate equivalence of day+1 on a year
const nextDayRatioed = tmrTimestamp / ratio
console.log(`ts_ratioed: ${Math.round(nextDayRatioed)}`)
console.log(`ts_next_year: ${getTime(nextYear)}`)
console.log(`next_year: ${nextYear.toLocaleString()}`)
console.log(`tmr_relative: ${new Date(Math.round(nextDayRatioed)).toLocaleString()}`)

输出:

wrong output

我如何才能使 tmr_relative 成为大约 2022 年 1 月的正确值

2 个答案:

答案 0 :(得分:1)

你必须保持不变

  • 作为起点的游戏开始时间。
  • 您想要的时间比例。在您的情况下,1 周是您的游戏的 1 年。

检查以下方法以仅使用日期来实现。

const ratio = 365/7;   //This is the virtual ration that you want
const nowReal = new Date() //This would always be the basis to compare

//Use a fixed date one week later to test how it would behave
const nextWeekReal = new Date();
nextWeekReal.setDate(nextWeekReal.getDate() + 7); 

//Use a fixed date 2 week later to test how it would behave
const doubleNextWeekReal = new Date();
doubleNextWeekReal.setDate(doubleNextWeekReal.getDate() + 14); 

//Check the next week virtual date
console.log(virtualDate(nowReal, datediff(nowReal, nextWeekReal), ratio));

//Check after 2 weeks the virtual date
console.log(virtualDate(nowReal, datediff(nowReal, doubleNextWeekReal), ratio));


function datediff(first: any, second: any) {
    // Take the difference between the dates and divide by milliseconds per day.
    // Round to nearest whole number to deal with DST.
    return Math.round((second-first)/(1000*60*60*24));
}

function virtualDate(basis: Date, diff: number, ration: number){
  const virtualDate = new Date();
  virtualDate.setDate(basis.getDate() + diff * ratio);
  return virtualDate;
}

结果考虑到您现在在 24/7/21 开始游戏。

实时过去 1 周后,它将在 1 年后从原点打印您

实时过去 2 周后,它将在 2 年后从原点打印您

enter image description here

<块引用>

假设我有一个活动持续 1 周(现实生活)在游戏中 将是 1 年。如果我在参加活动时尝试获取游戏中的日期 开始 + 3 天(活动已过近一半)。理论上我 应该接近6个月

//Use a fixed date half a week later to test how it would behave
const halfWeekReal = new Date();
halfWeekReal.setDate(halfWeekReal.getDate() + 3); 
console.log("Half Week have passed in real time " + halfWeekReal);

//Check after half week the virtual date
console.log("Virtual date will be " + virtualDate(nowReal, 
datediff(nowReal, halfWeekReal), ratio));

这将打印

enter image description here

大约是 5 个月,这是您描述的正确行为。

答案 1 :(得分:0)

这是一个使用普通老式 JS 的解决方案。

let refInGameDate = new Date()
document.querySelector('#days').addEventListener('change', e => {
  let days = +e.target.value
  let years = (Math.floor(days / 7) + (days % 7 / 7)).toFixed(2)
  document.querySelector('#ingame').innerText = years
  let rd = refInGameDate.getTime() + (years * 365 * 24 * 60 * 60 * 1000)
  document.querySelector('#indate').innerText = new Date(rd).toString();
})
<input type='number' id='days' /> real-time days
<hr>
<div>in-game translation: <span id='ingame'></span> years</div>
<div>date representation: <span id='indate'></span></div>

相关问题