Ayush
Ayush

Reputation: 42450

Output Date Without Slashes

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

Answers (4)

Andrew Collins
Andrew Collins

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

wageoghe
wageoghe

Reputation: 27618

Try this:

  DateTime d = DateTime.Now;
  string s = d.ToString("yyyyMMdd");

Upvotes: 2

Oded
Oded

Reputation: 499202

Simply use a custom datetime format string.

myDate.ToString("yyyyMMdd");

Upvotes: 8

D'Arcy Rittich
D'Arcy Rittich

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

Related Questions