如何以小时、分钟、秒、毫秒的格式获取电脑的时间?
本文关键字:获取 电脑 时间 格式 分钟 小时 | 更新日期: 2023-09-27 18:18:24
private void timer1_Tick(object sender, EventArgs e)
{
Image mScreenImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
ffmp.PushFrame((Bitmap)mScreenImage);
mScreenImage.Dispose();
}
我有这个定时器滴答事件的间隔为40ms。我想获得每次的时间时钟时间的pc每40ms。
例如,如果现在的时间是1:00 AM,那么我想看到每40毫秒:
1:00:00:40然后1:00:00:80等等
除此之外,我还想制作一个计时器,它将向前走,并在计时器滴答事件中显示每40ms。
00:00:00:40 00:00:00:80…
如何在计时器滴答事件中做到这一点?
使用to字符串输出日期时间以格式化。
var time = DateTime.Now.ToString("HH:mm:ss.ff");
将h
替换为小时,m
替换为- s
替换为秒,f
替换为几分之一秒(毫秒)。您可以进一步更改此格式以满足您的需要。
有关详细信息,请参阅MSDN文档
如何获取时间:
string formatedTime = DateTime.Now.TimeOfDay.ToString("HH:mm:ss:ff");
您可以将DateTime.Now
属性与格式说明符一起使用:
DateTime.Now.ToString("HH:mm:ss:ff")
你必须设置你的计时器
timer.Interval = 400;
和你的计时器滴答事件…假设在你的标签…
void timer_Tick(object sender, EventArgs e)
{
label.Text = Label1.Text = Format(Now, "HH:mm:ss:ff");
}