Reputation: 42450
I need to create a file that has today's date in the file name. How can I get the date just as 20111110
and no slashes?
Upvotes: 18
Views: 33539
Reputation: 2701
how about something like this:
System.IO.FileInfo file = new System.IO.FileInfo(File1.PostedFile.FileName);
string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
fname = fname + Now.ToString("_MMddyyyy_HHmmss") + file.Extension;
This creates the filename with the embedded datetime. Then you can prepend you folder path and pass it to SaveAs.
Upvotes: 7
Reputation: 27618
Try this:
DateTime d = DateTime.Now;
string s = d.ToString("yyyyMMdd");
Upvotes: 2
Reputation: 499202
Simply use a custom datetime format string.
myDate.ToString("yyyyMMdd");
Upvotes: 8
Reputation: 171509
DateTime.Now.ToString("yyyyMMdd")
Also, you may not be be aware of System.IO.Path.Combine
. It makes building paths a little cleaner and more foolproof.
Upvotes: 41