Click here to Skip to main content
12,763,360 members (25,098 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#

I need to change a date format.
I am getting the date from DB as 28-JUN-11, and I need to convert this as mm/dd/yyyy.
I need this in a string variable only.

I have tried with below code but getting the exception below
"Cannot unbox 'inObj' as a 'System.DateTime'"

I am reading the date from the DataReader as below.

private string GetDate(object inObj)
    return inObj == DBNull.Value ? "" : ((DateTime)inObj).Date.ToString("mm/dd/yyyy");

Posted 28-Jun-11 3:46am
SanjeevSingh 28-Jun-11 8:53am
why you are taking value in object. if you are using SQlDataReader you can directly get the value in string. after try using parse. hope it will help you.
monu_1983 28-Jun-11 9:02am
I am using OracleDataReader and not sure whether I can get string from that.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

See here: Formatting a DateTime for display - format string description[^]

Re-reading the question makes me think again: How are you actually reading it from the database? Did you store it as a string, or a DateTime? Because, if you stored it as a string, then that was a mistake, and you will have to Parse (or ParseExact) the string to a DateTime - you cannot cast it.

The reason I ask is that normally, SQL will only handle dates in ISO format: yyyy-MM-dd, so your date looks kinda odd.
monu_1983 28-Jun-11 9:05am
I am using oracle and reading data using oracle data reader, in DB, date field is varchar2
OriginalGriff 28-Jun-11 9:22am
Yep - you saved it as a string. Try to avoid that (use a date field, they are much more amenable to manipulation) - if nothing else, you can compare two dates directly - you can't do that with strings. It also helps because a DateTime is culture independant - the string you get from your user may not be!
You will need to use the DateTime.Parse (or DateTime.ParseExact) to convert it to a DateTime before you re-format it back into a numeric month date.
Vivek Krishnamurthy 28-Jun-11 9:30am
My 5!
Marcus Kramer 28-Jun-11 9:59am
Agreed. +5.
BobJanova 28-Jun-11 11:28am
Good answer.

When you've made this mistake (storing things in a DB as string) you should use ParseExact. Parse can run into locale issues if your saved date format is ambiguous. That's not a problem for this question but in general it can be.
monu_1983 29-Jun-11 1:04am
Thanks for a nice explanation
OriginalGriff 29-Jun-11 3:02am
You're welcome!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hope this[^] might help you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


return inObj == DBNull.Value ? "" : (Convert.ToDateTime(inObj)).ToString("MM/dd/yyyy").ToString();

Just a small note on your conversion: "mm/dd/yyyy"
mm stands for minutes, whereas MM stands for month.

Take care
monu_1983 28-Jun-11 9:52am
This works but problem is it does not change the date separator, my incoming date contains - as date separator(dd-mm-yyyy), I need / as date separator(MM/dd/yyyy).
Can u please help in this?
UJimbo 28-Jun-11 10:02am
Just to get this straight, your question is about changing how the date value is represented inside the database?
monu_1983 28-Jun-11 10:06am
No, I am reading the date from database, which comes in (dd-mm-yyyy) format, I need to convert that to (MM/dd/yyyy) format, I am reading the data from database using Oracle data reader and date is stored as a string in db.
UJimbo 28-Jun-11 10:16am
The above will return an empty string if there is no date value, else it will return e.g. 06/28/2011. You can try:
return inObj == DBNull.Value ? "" : "(" + (Convert.ToDateTime(inObj)).ToString("MM/dd/yyyy").ToString() + ")";
to add the parenthesis
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

try this
Data format string in c#[^]

Rate this: bad
Please Sign up or sign in to vote.

Solution 5

In addition to Griff's good answer:

There are two steps here. First is reading dates out of the database – if they're stored as dates, this is easy, but in your case you must parse them:
DateTime ParseDate(string dbcell) {
 return DateTime.ParseExact(dbcell, "dd-MMM-yyyy", CultureInfo.CurrentCulture); 

You need to do this before you can do anything useful with the dates on the client side.

Second is formatting them. You should only need to do this to (i) put them back into the database (using the same format as you are reading them back in, obviously), or (ii) display them to a user. It is an important point: within your application dates should be dates, not strings, except for the very moment of putting them into a string context. In the first case you should be using the format you quoted (dd-MMM-yyyy), and in the second case you should be using the user's default format (i.e. just use DateTime.ToString()) or, if for a web application, using a date format which is stored with the user's profile.

There is rarely a reason to enforce a particular date format on users (particularly one which is US-centric and ambiguous for the first 12 days of each month).
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

//Use MM in Capital letter
txtStartDate.Text = DateTime.Today.ToString("dd/MM/yyyy");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100