0

I have a textbox that should inserting a date in dd/MM/yyyy format. What only I can find and use is smalldatetime or datetime. I pick smalldatetime and the data displayed was like for example 01/07/2011 12:00:00 AM. Any codes involved to sort this out to be only displaying 01/07/2011 (without time)?

SqlJobMaster.InsertParameters["StartDate"].DefaultValue = txtStartDate.Text.ToString();

ASP.NET

Start Date

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
Com.Man.Do.Girl
  • 177
  • 2
  • 4
  • 16

4 Answers4

2

use ToString("dd/MM/yyyy");. Then it will get formatted the way you want it to.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
1

I haven't used GridView in ASP.NET, but according to this article you basically need to set the DataFormatString property for the relevant BoundField, e.g. to "{0:dd/MM/yyyy}". You should consider cultural variations though, if your app is to be used in multiple cultures: "{0:d}" is the general "short date pattern for the current culture" format string.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

something like this:

String.Format("{0:MM/dd/yyyy}", dt);            
Ivan Crojach Karačić
  • 1,911
  • 2
  • 24
  • 44
  • where to put this? mine DateTime dt = DateTime.Parse(txtStartDate.Text); SqlJobMaster.InsertParameters["StartDate"].DefaultValue = dt.ToShortDateString(); still not working – Com.Man.Do.Girl Jul 01 '11 at 09:38
  • `string s = String.Format("{0:MM/dd/yyyy}", dt);` this is just when you want to display it... if you store it into your database there is no need to do such things – Ivan Crojach Karačić Jul 01 '11 at 11:08
0

You can try :

DateTime dt = DateTime.Parse(txtDate.Text);
SqlJobMaster.InsertParameters["StartDate"].DefaultValue = dt.ToShortDateString();
2GDev
  • 2,478
  • 1
  • 20
  • 32
  • DateTime dt = txtStartDate.Text; when I declare this, it showing cannot implicitly convert type'string' to 'System.DateTime' – Com.Man.Do.Girl Jul 01 '11 at 09:30
  • sorry... DateTime.Parse(txtDate.Text) – 2GDev Jul 01 '11 at 09:30
  • still not working.Still displaying the time. here's my code DateTime dt = DateTime.Parse(txtStartDate.Text); SqlJobMaster.InsertParameters["StartDate"].DefaultValue = dt.ToShortDateString(); – Com.Man.Do.Girl Jul 01 '11 at 09:34