Click here to Skip to main content
15,877,296 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

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.

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


Thanks
Posted
Comments
[no name] 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.
maheshk_blr 28-Jun-11 9:02am    
I am using OracleDataReader and not sure whether I can get string from that.

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.
 
Share this answer
 
v2
Comments
maheshk_blr 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.
maheshk_blr 29-Jun-11 1:04am    
Thanks for a nice explanation
OriginalGriff 29-Jun-11 3:02am    
You're welcome!
Vivek Krishnamurthy 28-Jun-11 9:30am    
My 5!
Hope this[^] might help you.
 
Share this answer
 
try this
Data format string in c#[^]

-Ambi-
 
Share this answer
 
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).
 
Share this answer
 
try

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
 
Share this answer
 
v5
Comments
maheshk_blr 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?
maheshk_blr 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
C#
//Use MM in Capital letter
txtStartDate.Text = DateTime.Today.ToString("dd/MM/yyyy");
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900