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

How to get future date based on timestamp ratio

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

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

假设我有一个活动 运行 1 周 (现实生活) 在游戏中 1 年 。 如果我在 事件开始 + 3 天 时尝试获取 in-game 日期(几乎一半的事件已通过)。从理论上讲,我应该在虚拟日历中接近 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()}`)

输出:

我怎样才能让 tmr_relative 成为正确的值,大约是 2022 年 1 月

这是一个使用普通旧版 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>

你必须保持不变

  • 你的游戏开始时间作为原点。
  • 你想要的时间比例。在你的情况下,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 年后的你

Lets say I have an event running for 1 week (real life) In game that would be 1 year. If I try to get the in-game date when I'm at event start + 3 days (almost half of the event passed). Theoretically I should be close to 6 months

//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));

这将打印

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