tags:

views:

68

answers:

2

My C# application send a formatted date (ex: 9/6/2010 - dd/mm/yyyy) to an excel spreadsheet... but then... it comes to 6/9/2010! (mm/dd/yyyy)

I just use the following code to send the date:

VarRowColumnWhatever = _MyList.MyObject.MyDateTime.Date.ToString();

Debbuging it, i can see that the value are correct! But not in the spreadsheet =(

I began to think that is something to do with Excel...

Someone please can help me? Thanks!

+1  A: 

Stick a string according to the pattern you want into the ToString() portion.

For instance, to get the Full Day, Month, Date, Year, i'd put:

_MyList.MyObject.MyDateTime.TimeOfDay.ToString("D");

which would output the string: "Wednesday, June 23, 2010"

// This example displays the following output to the console:
// d: 6/15/2008
// D: Sunday, June 15, 2008
// f: Sunday, June 15, 2008 9:15 PM
// F: Sunday, June 15, 2008 9:15:07 PM
// g: 6/15/2008 9:15 PM
// G: 6/15/2008 9:15:07 PM
// m: June 15
// o: 2008-06-15T21:15:07.0000000
// R: Sun, 15 Jun 2008 21:15:07 GMT
// s: 2008-06-15T21:15:07
// t: 9:15 PM
// T: 9:15:07 PM
// u: 2008-06-15 21:15:07Z
// U: Monday, June 16, 2008 4:15:07 AM
// y: June, 2008
//
// 'h:mm:ss.ff t': 9:15:07.00 P
// 'd MMM yyyy': 15 Jun 2008
// 'HH:mm:ss.f': 21:15:07.0
// 'dd MMM HH:mm:ss': 15 Jun 21:15:07
// '\Mon\t\h\: M': Month: 6
// 'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00

Caladain
The question actually has to do with this patterns send by "ToString", and it works! But in my case i've just remove the "ToString", send only the "Date" and it works too... i'm feeling a little 'owned' after that =( Thanks and regards!
Kira
+1  A: 

Try to write your code like this:

VarRowColumnWhatever = _MyList.MyObject.MyDateTime.Date.ToString("dd\/MM\/yyyy");

or make sure your culture is set to one with your desired format.

If the Excel cell you are putting the value into is of Date type in Excel (if using interop), then you should use:

VarRowColumnWhatever = _MyList.MyObject.MyDateTime.Date.ToOADate();
Mikael Svenson
I've try this too, but like i said before, I just haven't thought in the possibility of remove the ToString... curiously (or not) it worked for my case. From now i'll pay more attention in some details LOL =D
Kira