Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i try use DateTime format show in "dd/MM/yyyy HH:mm:ss" Like "06/06/2020 20:06:55"

but i failed can any one help me

my code is :

String date = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
DateTime fullDate = Convert.ToDateTime(date);


string fileSavepath = "" + folderPath + "Report -" + fullDate.Day + "-" + fullDate.Month + "-" + fullDate.Hour + "-" + fullDate.Minute + "-" + fullDate.Second + ".xlsx";

wb.SaveAs(fileSavepath);

What I have tried:

when i use debugging
my first string output datetime correct format, but after second datetime fullDate output wrong format

i want when i save file it's save "Report -06-06-20-06-55" but it's save now "Report -6-6-20-6-55"
Posted
Updated 6-Jun-20 5:13am
Comments
MadMyche 6-Jun-20 12:08pm    
Just a recommendation... I would format using one a canonical datetime patterns which will allow for natural sorting by name as well as date.
I personally use something along the lines of Filename_20201231-181005.ext
Amar chand123 6-Jun-20 22:43pm    
Thank you i try
KarstenK 6-Jun-20 13:36pm    
Nice tip - if I write files I do it similar.

Why are you converting DateTime.Now to a string just to convert it back to a DateTime object? All you need to do to create your filename is:
C#
string filename = string.Format("Report -{0}.xlsx", DateTime.Now.ToString("dd-MM-hh-mm-ss"));
string fileSavepath = Path.Combine(folderPath, filename);
 
Share this answer
 
Comments
Amar chand123 6-Jun-20 11:38am    
Thank you for answering sir Richard and why i convert string to datetime and again datetime because when i try Google i found some said so i just try
Richard MacCutchan 6-Jun-20 11:53am    
Don't rely on Google to write your code. Spend some time actually learning the language.
Amar chand123 6-Jun-20 12:11pm    
I am new in language so when i have any problem i try search solutions on Google and now i everyday spend one hour on YouTube for learning language
Richard MacCutchan 6-Jun-20 12:15pm    
Well all those hours are probably wasted. Get yourself a proper book on C# and learn from there.
Amar chand123 6-Jun-20 12:22pm    
Thank you for suggestions now I think i really need a book of learning language after lockdown i go and buy a book
Why?
DateTime.Now is already a DateTime value ... so why convert that to a string, just to convert it back to a DateTime - especially as you specify a format to the ToString but use the system default for the Convert?
Just use DateTime.Now directly:
DateTime now = DateTime.Now;
string formattedDate = $"Report -{now.Day:00}-{now.Month:00}-{now.Hour:00}-{now.Minute:00}-{now.Second:00}";
string fileSavePath = Path.Combine(folderPath, formattedDate + ".xlsx");
Note: I copy DateTime.Now into a variable - this is important as multiple reads of DateTime.Now can give consistency problems around midnight!
 
Share this answer
 
v2
Comments
Amar chand123 6-Jun-20 11:34am    
First i try only datetime filldate = datetime.now but it's not working than i try Google for solutions then i find this so i try but its also not work then i place question here because here i always learn new
OriginalGriff 6-Jun-20 11:53am    
C# is case sensitive:
datetime filldate = datetime.now
is not valid:
DateTime filldate = DateTime.Now;
Is.
Amar chand123 6-Jun-20 12:15pm    
Sir i try DateTime filldate = DateTime.Now; in this comment i only typed in smallcase

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