将当前时间戳分配给变量

本文关键字:变量 分配 时间戳 | 更新日期: 2023-09-27 18:05:13

我试图将一些数据时间戳的接收存储到这样的变量:

DateTime timetora = DateTime.Now;
DateTime receptiontimestamp;
receptiontimestamp = timetora;

,但我认为timetora总是在进步,receptiontimestamp也是如此。但我希望它保持不变,指向接待的时刻,而不是现在。我做错了什么?

将当前时间戳分配给变量

不会继续"progress "

当你这样做的时候:

DateTime timetora = DateTime.Now;

timetora现在固定为该时间(以及您设置为timetora的任何其他DateTime),您所写的内容没有问题。

您给出的代码将复制timetorareceptiontimestamp的值。它不会继续前进。例如:

DateTime before = DateTime.Now;
Thread.Sleep(10000);
DateTime after = before;
Console.WriteLine("Before: {0}", before);
Console.WriteLine("After: {0}", after);

两行将显示相同的时间——它们不会显示10秒的差异。据我所知,这是你想要的,对吧?

DateTime是一个值类型,所以

DateTime timetora = DateTime.Now;

创建当前DateTime的副本。Timetora在以后访问时不会增加。