-3

I tried converting this

DateTime Todate = DateTime.ParseExact("22/08/2012", "dd/MM/yyyy", null);

got output :

8/21/2012 12:00:00 AM

How can i get date output in dd/MM/yyyy

EDIT : I am getting the text from calendar textbox as string as "22/08/2012" and now I need to convert this to dateTime Datatype to insert in to DB through DAL Class variable which is in DateTime DataType

string[] f1 = datepicker1.Text.Split(' ');
string[] t1 = datepicker2.Text.Split(' ');
DateTime Fromdate1 = DateTime.ParseExact(f1[0], "dd/MM/yyyy", CultureInfo.InvariantCulture);
//Convert.ToDateTime(datepicker1.Text);
DateTime Todate1 = DateTime.ParseExact(t1[0], "dd/MM/yyyy", null);
ObjSeasonPrice.SeasonPriceName = txtSeasonPriceName.Text.Trim();
ObjSeasonPrice.PropertyId = Convert.ToInt32(propId.ToString());
ObjSeasonPrice.RoomId = Convert.ToInt32(roomId.ToString());
ObjSeasonPrice.RatePerNight = Convert.ToDecimal(txtRatePerNight.Text);
ObjSeasonPrice.Days = getAllDaysWithComma();
ObjSeasonPrice.AdditionalBenefits = txtAdditionalBenifits.Text.Trim();
ObjSeasonPrice.Status = ddlStatus.SelectedItem.ToString();
ObjSeasonPrice.IsDeleted = Convert.ToBoolean("False");
ObjSeasonPrice.FromDate = Fromdate1;
ObjSeasonPrice.ToDate = Todate1;

Sorry for low info,but why you guys keep downvoting without taking time to understand the issue completely.

Julian
  • 20,008
  • 17
  • 77
  • 108
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
  • Please add an tag which descripes the programmer language do you use – Reporter Aug 21 '12 at 12:01
  • dont down vote this guys..I tried putting invarian culture also DateTime Fromdate1 = DateTime.ParseExact(f1[0], "d/MM/yyyy", CultureInfo.InvariantCulture); – sajanyamaha Aug 21 '12 at 12:04
  • Simply because it is a 'basic' question, does not mean it deserves downvotes...What he needs is an explanation especially since he appears to be new to SO – Amit Mittal Aug 21 '12 at 12:06
  • 1
    You write "got output : 8/21/2012 12:00:00 AM", but you don't show us the code generating this output. You should include that in your question as well. – Julian Aug 21 '12 at 12:28
  • 1
    It seems you have no problems in your code, just in understanding your debugger and/or logfile. – H H Aug 21 '12 at 12:32
  • in 3rd line : DateTime Fromdate1 = DateTime.ParseExact(f1[0], "dd/MM/yyyy", CultureInfo.InvariantCulture); breakpoint gives f1[0] = "22/08/2012" But Fromdate1 = "8/22/2012 "12:00:00 AM – sajanyamaha Aug 21 '12 at 12:38
  • @sajanyamaha: `22/08/2012` contains _only_ the date part (and _not_ the time part) of the point in time. When parsing this string, your `DateTime` object _will_ contain the default time part (which is midnight (00:00 using a 24h clock, or 12 AM using a 12h clock)). Therefore it is completely correct that parsing `22/08/2012` results in the `DateTime` object `8/22/2012 "12:00:00 AM`. – Julian Aug 21 '12 at 12:45
  • @Nailuj Yes I can understand the time part,but wat about the change in **22/08/2012 => 8/22/2012** – sajanyamaha Aug 22 '12 at 03:59
  • 1
    @sajanyamaha, `8/22/2012` is just the way Visual Studio shows you the date in the debugger. As mentioned in other comments, `DateTime` doesn't store dates in a particular format - it simply stores years, days, hours, minutes, seconds etc, so your code is fine. – Simon MᶜKenzie Aug 22 '12 at 06:51

2 Answers2

3

Try this:

DateTime Todate = DateTime.ParseExact("22/08/2012", "dd/MM/yyyy", null);
Todate.ToString("dd/MM/yyyy"); // output in your chosen format.

The point is that Todate is a DateTime object, so it actually stores the date and time internally as a big number. If you want to display it in a certain format, then you convert it to a string. See here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

EDIT: If you want to change the default display format for DateTime then see here: Set Default DateTime Format c#

Community
  • 1
  • 1
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
  • Dommer,I need this as DateTime Datatype itself. – sajanyamaha Aug 21 '12 at 12:06
  • `Todate` is an object of `DateTime` datatype. Your code is correct. If you want it to be output in a certain way, you specify that when you convert it to a string. The point is that `DateTime` doesn't have a display format. – Tom Chantler Aug 21 '12 at 12:07
  • 2
    @sajanyamaha `DateTime` doesn't have an output format on its own. It is an object with numerous properties including `Day`, `Month`, and `Year`. The output format is decided when you call `ToString()` or some other method. Why don't you try to explain what you are trying to do that isn't working? – psubsee2003 Aug 21 '12 at 12:09
  • 2
    If you want to change the default way of displaying DateTime, see here: http://stackoverflow.com/questions/1389187/set-default-datetime-format-c-sharp – Tom Chantler Aug 21 '12 at 12:10
  • @sajanyamaha DateTime is a class that encapsulates the concept of both date and time together (as you can guess from the name of the class). while parsing the date you specified the format 'dd/MM/yyyy'. This format was the format of the input text. Notice that you get time part as 12:00 AM. This is the default time (as your input text didn't consist of any time part). You can continue to use the parsed date as you wish but when you want to convert it back to string (for output), you need to provide the required format again. – Amit Mittal Aug 21 '12 at 12:12
  • @psubsee2003 I am getting the text from calendar textbox as string as "22/08/2012" and now I need to convert this to dateTime Datatype to insert in to DB through DAL Class variable which is in DateTime DataType. – sajanyamaha Aug 21 '12 at 12:13
  • @sajanyamaha you should better include the DB part in the question itself and also a code snapshot of DAL showing us how you are putting your date in DB – Amit Mittal Aug 21 '12 at 12:15
  • 2
    @sajanyamaha maybe I am missing something, but I still don't see the issue with your DateTime object... when you parse the string, you get a DateTime object with `DateTime.Day = 22`, `DateTime.Day = 8` and `DateTime.Year = 2012`, correct? If so, then the parse is working correctly. If something is not working when it is writing to the DB, then I think you need to look at that code as the source of the problem. – psubsee2003 Aug 21 '12 at 12:17
  • 1
    sorry... typo above that I missed after the 5 min window was up, should be `DateTime.Month = 8` – psubsee2003 Aug 21 '12 at 12:24
0
Todate.ToString("dd/MM/yyyy")

Will output the date in the format you specified

628426
  • 373
  • 3
  • 14