简体   繁体   English

在DateTime值中转换Cookie字符串

[英]Convert Cookie string in DateTime value

I need convert one Cookie string value in DateTime value in c#. 我需要在c#中的DateTime值中转换一个Cookie字符串值。

The cookie is valid because if try this: cookie有效,因为如果尝试这样做:

Response.Write(Request.Cookies["dt"].Value + "<br />");

In output I have: 在输出中,我有:

08/07/2015

I can't convert this Cookie string on DateTime. 我无法在DateTime上转换此Cookie字符串。

I have tried this method: 我试过这种方法:

DateTime DataCookie;    
DataCookie = DateTime.ParseExact(Request.Cookies["dt"].Value, "yyyy-MM-dd", CultureInfo.InvariantCulture);

But I have thie error: 但是我有错误:

String was not recognized as a valid DateTime. 无法将字符串识别为有效的DateTime。

Wath's a wrong? 错了吗?

Clearly, your format and string doesn't exactly match. 显然,您的格式和字符串不完全匹配。

From documentation ; 文件 ;

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. 使用指定的格式和特定​​于区域性的格式信息,将日期和时间的指定字符串表示形式转换为其等效的DateTime。 The format of the string representation must match the specified format exactly. 字符串表示形式的格式必须与指定的格式完全匹配。

Use dd/MM/yyyy format instead. 请改用dd/MM/yyyy格式。

DataCookie = DateTime.ParseExact(Request.Cookies["dt"].Value,
                                 "dd/MM/yyyy", 
                                 CultureInfo.InvariantCulture);

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

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