Asp.net C#Web应用程序中的日期问题 [英] Date Problem in Asp.net c# web application

查看:58
本文介绍了Asp.net C#Web应用程序中的日期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在.net framework 4和后端sql server 2008中开发Web应用程序,其中

我以这种方式将日期保存在我的表中

i am developing a web application in .net framework 4 and back end sql server 2008 in which

I m Saving Date In My table in this way

System.DateTime str_date = DateTime.ParseExact(txtOrderdt.Text, "dd/MM/yyyy", null);

sql = "insert into RetailerOrder (OrderDate) values (@OrderDate)

cmd.Parameters.AddWithValue("@OrderDate", str_date);


它在表格中保存为2012年3月12日上午12:00:00(2012年3月12日)

保存后,将此日期绑定到gridview,同时将列格式设置为{0:dd/MM/yyyy}.

我的网格还包含编辑"按钮,在编辑时单击显示下一页",然后将特定行的日期设置为具有ajax日历扩展器的文本框txtorderdt,如下所示.


it saves in table as 03/12/2012 12:00:00 AM(it is 12 march 2012)

after saving i m binding this date to gridview while binding i m setting column format as{0:dd/MM/yyyy}.

my grid also contain edit button,on edit click i m showing next page and thr i m setting that particular row''s date to textbox txtorderdt having ajax calender extender as shown below.

DateTime dtord;

dtord = Convert.ToDateTime(Session["OrdDate"].ToString());

txtOrderdt.Text =dtord.ToString("dd/MM/yyyy");

它显示我的日期为2012年3月12日,但我希望此处的日期为2012年12月3日

因此,请保存在表中时指导我格式化日期,以及如何以正确的格式检索它.

it show''s me date as 3/12/2012 but i want date here as 12/03/2012

so please guide me in formating date while saving in table and also how to retrive it in proper format

推荐答案

不要将它串起来.

而是将其作为DateTime存储在会话中,然后将其作为DateTime重新加载.

Don''t ToString it.

Instead, store it in your Session as a DateTime, and load it back as a DateTime.

dtord = (DateTime) Session["OrdDate"];

如果仅在DateTime上使用默认的ToString,它将使用当前的区域性-在Web服务器上可以是任何区域性,在这种情况下,几乎可以肯定是使用MM/dd/yyyy格式.

每次转换数据类型时,都有冒这样的错误的风险-尽快将它们放入DateTime并将它们保留为DateTime直到显示它们的最后一刻!

If you just use the default ToString on a DateTime, it will use the current culture - which on a web server could be anything and in this case is almost certainly using MM/dd/yyyy format.

Every time to convert datatypes, you run the risk of introducing errors like this - get them into DateTime as quickly as possible and leave them as DateTime until the last moment before you display them!


我们必须在sqlserver中更改日期格式.


通过使用SELECT CONVERT(VARCHAR(10),GETDATE(),103)可以显示

[DD/MM/YYYY]:2012年12月3日
we must change date format in sqlserver.


By using SELECT CONVERT(VARCHAR(10), GETDATE(), 103) it shows

[DD/MM/YYYY]:12/03/2012


这篇关于Asp.net C#Web应用程序中的日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆