0

I just can't seem to find an answer for this hence the post. I want the time stored in the SQL database to be displayed on my website to be the exact time that is in the database. Currently, this is not the case. The time being displayed is being converted to the browsing computers local time. Since the time being saved is an appointment time, regardless of where or what time zone the browsing computer is in, having it converted makes no sense. I thought that the methods TimeZone and TimeZoneInfo were used to convert the time to local time, but this seems to be happening automatically. How do I get the exact time saved to be the time displayed on my site?

The SQL database fields ETAStart and ETAEnd are defined as datetime.

Updates to the database are handled in the controller as such:

if (TryUpdateModel(model)) {

    System.DateTime ETAEnd = Convert.ToDateTime(model.ETAStart).AddHours(3);

    model.ETAEnd = ETAEnd;
    model.UpdatedBy = CurrentUser();

    model.UpdateDate = Convert.ToDateTime(DateTime.Now.ToString("U"));


    dc.SubmitChanges();


    return View(new GridModel<ManifestMasterModel> { Data = GetMMList(0) });

} else {
    return View();
}

Thank you in advance and I am very appreciative to all who respond.

4

3 回答 3

0

您是否尝试使用以下方式将日期时间存储为 UTC:

model.UpdateDate = Convert.ToDateTime(DateTime.Now.ToString("U"));

您可以像这样使用它来不进行转换:

model.UpdateDate = DateTime.Now.ToUniversalTime();
于 2012-04-21T20:28:05.017 回答
0

您是否检查了服务器上设置的时区?几天前发生在我身上,结果托管服务器有不同的时区。

以下是检查方法:

Response.Write(TimeZoneInfo.Local.DisplayName)

编辑1:

您可以使用 Utc 时间将其转换为您想要的时区。看:

TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
于 2012-04-21T20:05:44.123 回答
0

我不完全确定我理解问题所在,但在我看来,您需要实现自定义模型绑定器。

这些资源可能会帮助您:

时区策略

Asp.net MVC 中的自定义 DateTime 模型绑定器

http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx

虽然,我认为仅使用字符串的快速修复建议没有任何问题(我认为您会帮自己一个忙)。听起来您正试图让客户端和 MVC 忽略处理日期的时区影响,这可能需要更多的工作并且比仅使用字符串方法更不干净。您仍然可以在数据库中有一个 DateTime,只需提取用户提交的字符串值并使用适当的时区将其解析为日期时间。

于 2012-04-22T00:42:28.823 回答