简体   繁体   English

将DateTime存储到cookie中的最佳方法是什么?

[英]What is the best way to store DateTime into cookies?

I am working on an ASP .NET project, and currently I am using this code to store and retrieve DateTime on cookies: 我正在开发一个ASP .NET项目,目前我正在使用此代码在cookie上存储和检索DateTime:

HttpCookie cookie = new HttpCookie("myCookie");
if (Request.Cookies["myCookie"] != null)
{
    cookie = Request.Cookies["myCookie"];
}
else
{
    cookie["From"] = DateTime.Now.AddDays(-7).ToShortDateString();
    cookie["To"] = DateTime.Now.ToShortDateString();
    Response.Cookies.Add(cookie);
}
model.FromDate = DateTime.Parse(cookie["From"]);
model.ToDate = DateTime.Parse(cookie["To"]);

And in my View I am using Razor to recovery the model values like this: 在我的视图中,我使用Razor恢复模型值,如下所示:

<input type="text" id="from" value="@Model.FromDate.ToShortDateString()" readonly="readonly" />
<input type="text" id="to" value="@Model.ToDate.ToShortDateString()" readonly="readonly" />

It is working fine when I run it locally, but when I uploaded to production, when recovering the DateTime from cookie, the date is changed in one day. 当我在本地运行它时工作正常,但是当我上传到生产时,从cookie恢复DateTime时,日期会在一天内更改。 For example, I selected a date range from 3/25/2016 to 4/1/2016, then I have gone to another page, and when I came back to this page, the page showed a date range from 3/24/2016 to 3/31/2016, decreased in one day. 例如,我选择了从2016年3月25日到2016年4月1日的日期范围,然后我又转到了另一页,当我回到此页面时,页面显示的日期范围是2016年3月24日到2016年3月31日,一天减少。

Do you know what I am doing wrong here, why this just happens in production (I suppose is something related to server date) and, finally, what is the best way to store and retrieve a DateTime on cookie? 你知道我在这里做错了什么,为什么这只是在生产中发生(我想是与服务器日期相关的东西),最后,什么是在cookie上存储和检索DateTime的最佳方法?

You can store the date in Ticks 您可以将日期存储在Ticks中

cookie["From"] = DateTime.Now.AddDays(-7).Ticks.ToString();
cookie["To"] = DateTime.Now.Ticks.ToString();

and recover like this: 并像这样恢复:

model.FromDate = new DateTime(Convert.ToInt64(cookie["From"]));
model.ToDate = new DateTime(Convert.ToInt64(cookie["To"]));

Hope this helps you 希望这对你有所帮助

When storing date / time as string you should always consider timezones. 将日期/时间存储为string您应始终考虑时区。 I recommend you to use DateTimeOffset instead of DateTime : 我建议你使用DateTimeOffset而不是DateTime

var now = DateTimeOffset.Now;
var asString = now.ToString(CultureInfo.InvariantCulture);
var asDatetimeOffset = DateTimeOffset.Parse(asString, CultureInfo.InvariantCulture);

The string looks like this: 04/01/2016 22:01:09 +02:00 字符串看起来像这样: 04/01/2016 22:01:09 +02:00

(You need to know the client's timezone to correctly calculate its 'local' time. In your example, the server uses it's own time.) (您需要知道客户端的时区才能正确计算其“本地”时间。在您的示例中,服务器使用它自己的时间。)

The result of DateTime.Parse(cookie["From"]); DateTime.Parse(cookie["From"]);的结果DateTime.Parse(cookie["From"]); is set to DateTimeKind.Unspecified . 设置为DateTimeKind.Unspecified Any further operation (like AddDays) depends the system's timezone. 任何进一步的操作(如AddDays)都取决于系统的时区。

I think you should specify the culture and tell the parser which DateTimeKind to expect: 我认为你应该指定文化并告诉解析器期望DateTimeKind

model.FromDate = DateTime.Parse(cookie["From"], CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal);
model.ToDate = DateTime.Parse(cookie["To"], CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal);

Note : DateTime.ToShortDateString isn't a good choice, since it is defined by the current culture's DateTimeFormatInfo object. 注意 :DateTime.ToShortDateString不是一个好的选择,因为它是由当前文化的DateTimeFormatInfo对象定义的。

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

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