'C# WFA VS - speed up alternative in-game clock connected to local time DateTime.Now
so i've found some code, and applied to my needs, and it seems to work, but now i need to do the same stuff in C# (for and app) or to do an app for windows in javascript. any help? here's the code:
const speed = 7; // how many times faster than real time
let clockDiv = document.querySelector("#clock");
let gameStartTime = 10800000; // game-milliseconds;
let realStartTime = Date.now(); // real milliseconds
let timerId = setInterval(function() {
let gameTime = gameStartTime + Date.now() * speed;
let sec = Math.floor(gameTime / 1000) % 60;
let min = Math.floor(gameTime / 60000) % 60;
let hour = Math.floor(gameTime / 3600000) % 24;
// output in hh:mm:ss format:
clockDiv.textContent = `${hour}:${min}:${sec}`.replace(/\b\d\b/g, "0$&");
}, 50);
<div id="clock" style="font-size:40pt;"></div>
Solution 1:[1]
C# Winform:
MainDesign:
Control required:
- Label*1
- Timer*1
Timer:
Code:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static long currentTimeMillis()
{
return (System.DateTime.UtcNow.Ticks - Jan1st1970Ms) / 10000;
}
private static long Jan1st1970Ms = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc).Ticks;
private void timer1_Tick(object sender, EventArgs e)
{
const int speed = 7;
int gameStartTime = 10800000;
Double realStartTime = currentTimeMillis();
var gameTime = gameStartTime + realStartTime * speed;
var sec = Math.Floor(gameTime / 1000) % 60;
var min = Math.Floor(gameTime / 60000) % 60;
var hour = Math.Floor(gameTime / 3600000) % 24;
label1.Text = string.Format("{0:00}:{1:00}:{2:00}", hour, min, sec);
}
}
}
Output:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jiale Xue - MSFT |




