Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows;

xlsheet.Cells[1, 1] = "printed date" + DateTime.Now;

when i use the above code, my output as follows;

printed date 3/5/2013 5:37:37 PM.

the above output i retrieve in excel.

But i want the output as follows in excel;

Printed date 5/Mar/2013.

for the above output,what change i can do to get my above output.


Regards,
Narasiman P.
Posted

Look here: http://www.csharp-examples.net/string-format-datetime/[^]
Btw: dd will give 05, not 5, and your format specified in the title does not match the sample - so your question is discrepant.
C#
String.Format("{0:dd-MMM-yyyy}", DateTime.Now)

C#
String.Format(@"{0:dd\/MMM\/yyyy}", DateTime.Now)

Note, that you might need to specify culture info too...
 
Share this answer
 
v2
Comments
Maciej Los 5-Mar-13 11:26am    
Good answer, +5!
also try this code
xlsheet.Cells[1, 1] = "printed date" + DateTime.Now.ToString("DD/MMM/YYYY");
 
Share this answer
 
v2
Comments
Maciej Los 5-Mar-13 11:25am    
Good answer, +5!
Hi,
Formatting the DateTime to specific string will not display as expected in the excel file, because the Excel cell format will override it.

you need to format the cell not the date time.

C#
Microsoft.Office.Interop.Excel.Range range = worksheet.Cells[1,1] as Range;
range.NumberFormat = "dd/MMM/yyyy";
range.Value2 = DateTime.Now;


Regards
Jegan
 
Share this answer
 
v2
Comments
Maciej Los 5-Mar-13 11:24am    
Sorry, Jegan, because you're wrong. Please, carefuly read question.
xlsheet.Cells[1, 1] = "printed date" + DateValue;
Do you see it, now? Format of cell is ignored in this case.
Jegan Thiyagesan 5-Mar-13 11:33am    
My apology, I missed the piece of string in front.

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