简体   繁体   English

System.DateTime客户端依赖吗?

[英]Is System.DateTime client dependant?

I was working on an attendance system on ASP.Net where the entire week's dates are calculated by my program based on the current date. 我正在ASP.Net上的出勤系统上工作,该程序根据我的程序基于当前日期计算整个星期的日期。 I am using DateTime.Today. 我正在使用DateTime.Today。 I was apprehensive to use this as I feel that users can modify this date by merely changing their system clock and mark previous week's attendance. 我对此感到不安,因为我觉得用户可以通过仅更改系统时钟并标记前一周的出勤来修改日期。

Can DateTime.Today be changed by changing system clock on client side? 可以通过更改客户端的系统时钟来更改DateTime.Today吗? If yes, is there something else I can use instead of that? 如果是,还有其他可以代替的东西吗?

DateTime.Today will return the value on the computer it is run on. DateTime.Today将在运行它的计算机上返回该值。

In an ASP.NET page, this will be the server. 在ASP.NET页中,这将是服务器。

There is no way that the client browser will be able to modify this value. 客户端浏览器无法修改该值。

If you are using the class within a client based technology (say silverlight), or using a custom client application and using DateTime.Today within it directly, then yes, the client date/time will be used and could be changed by the client. 如果您在基于客户端的技术(例如Silverlight)中使用该类,或者在自定义客户端应用程序中直接使用DateTime.Today ,则可以使用客户端的日期/时间,并且客户端可以更改该日期/时间。

DateTime.Now will always give the time used on the server and not on client computer. DateTime.Now将始终提供服务器而不是客户端计算机上使用的时间。

You can also use DateTime.UtcNow 您也可以使用DateTime.UtcNow

在ASP.NET应用程序中,DateTime.Today应该是服务器上的日期/时间,并且不能由最终用户调整,因此应该没问题。

You should have all of your times in UTC format, on the server. 您应该将所有时间都以UTC格式保存在服务器上。 Use DateTime.UtcNow . 使用DateTime.UtcNow I would suggest keeping the user's timezone offset in your database somewhere and then adjusting from there for display purposes. 我建议将用户的时区偏移量保留在数据库中的某个位置,然后从那里进行调整以用于显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM