如何在C#Asp.Net中将会话值分配给日期时间变量? [英] How Do I Assign Session Value To Datetime Variable In C# Asp.Net?
问题描述
我想将日期时间值从应用程序传递到存储过程(SP)。
用户的上次登录日期存储在会话变量中,我想在SP中传递它。
i我这样做 -
DateTime myDt = DateTime.ParseExact(会话[ Last_Login_date] .ToString(), yyyy-MM-dd,System.Globalization.CultureInfo.InvariantCulture);
但我收到错误 -
字符串未被识别为 一个有效的DateTime。
我的会话[Last_Login_date]包含日期 - 2014年6月30日12:08:PM
我是否需要更改格式并分配?
请帮助
试试这个:
DateTime dt = DateTime.ParseExact(30,2014年6月12:08 PM,dd,MMM yyyy hh:mm tt,CultureInfo .InvariantCulture);
在此之前,请确保您的日期格式与上面带下划线的字符串中指定的一致。
使用< a href =http://msdn.microsoft.com/en-us/library/System.DateTime.TryParseExact%28v=vs.110%29.aspx> TryParseExact方法 [ ^ ]到检查日期和格式的有效性。
尝试使用
DateTime myDt = Convert.ToDateTime(Session [ Last_Login_date]);
如果你要使用DateTime.ParseExact
然后你需要给出确切的格式作为输入,否则它将失败。
直接用DateTime对象设置会话值。
会话[ < span class =code-string> Last_Login_date] = DateTime.Now;
当你需要取回值时,
DateTime myDt = Convert.ToDateTime(会话[ Last_Login_date ]);
Hi,
I want to pass datetime value from application to stored procedure (SP).
Last login date of user is stored in session variable and i want to pass it in SP.
i am doing it like this-
DateTime myDt = DateTime.ParseExact(Session["Last_Login_date"].ToString(), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
but i am getting error-
String was not recognized as a valid DateTime.
My Session["Last_Login_date"] contains date - "30, Jun 2014 12:08: PM"
Should i need to change format and assign?
Please help
Try this:
DateTime dt = DateTime.ParseExact("30, Jun 2014 12:08 PM", "dd, MMM yyyy hh:mm tt", CultureInfo.InvariantCulture);
Before this, make sure your date format is appropriate as specified in the string underlined above.
Use TryParseExact method[^] to check for validity of date and format.
try with
DateTime myDt = Convert.ToDateTime(Session["Last_Login_date"]);
if you going to useDateTime.ParseExact
then you need to give exact format as your input, otherwise it will fail.
Set session value with DateTime object directly.
Session["Last_Login_date"] =DateTime.Now;
when you need to retrieve value back,
DateTime myDt = Convert.ToDateTime(Session["Last_Login_date"]);
这篇关于如何在C#Asp.Net中将会话值分配给日期时间变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!