Date format conversion

Hey,

I am trying to convert a date pulled out from a data reader into the following format ‘dd/mm/yyyy’.

I have this line of code:


System.Convert.ToDateTime(dbReaderAppl["date_addedd"].ToString())

But how can i convert the date to this format?

Thanks

Thanks, just get a chance to fix it now :slight_smile:

Assuming dateaddedd is a date/time, why are you converting a date to a string and then back to a datetime?

Try this:

dbReaderAppl["date_addedd"].ToString("dd/MM/yyyy");

You do want the month in the middle, correct?

I “think” you’re using ToString() becuase the item is coming out of the reader boxed in an object, correct?

I know I have some lines of code that look like this. You’re completing the conversion to a DateTime correct? You just need to set it to the correct format you want?

Here’s a page with formatting info. You’ll note that you’re not actually setting the format of the DateTime, that just “IS”. You’re setting the formatting for the string when you try to write out the DateTime for display.

I get an error when using this, a red line.

Error: No overload for method ‘ToString’ takes ‘1’ arguments

Any ideas what i can do?

Thanks

I haven’t worked with date formatting before, but if you’re using any flavor of Visual Studio, it should provide you with Intellisense. It may not give you the different formats, it probably expects you to figure that value out for yourself but it should at least give you the overloaded methods including the number of parameters and their data type. Even when I’m hand coding in another app, an error like this is where I’ll pull the file up in VS, drop into code view and start typing it myself so I can see the valid method signatures. At least, that’s generally how I approach a problem like this.

This is what you need to do:

((DateTime)dbReaderAppl[“date_addedd”]).ToString(dd/mm/yyyy);

or

Convert.ToDateTime(dbReaderAppl[“date_addedd”].ToString()).ToString(dd/mm/yyyy);