43

I have had this issue quite a few times and have just been using a workaround but thought I would ask here in case there is an easier option. When I have a string from DateTime.Now and I then want to use this in a filename I can't because Windows doesn't allow the characters / and : in filenames, I have to go and replace them like this:

string filename = DateTime.Now.ToString().Replace('/', '_').Replace(':', '_');

Which seems a bit of a hassle, is there an easier way to do this? Any suggestions much appreciated.

Bali C
  • 30,582
  • 35
  • 123
  • 152

5 Answers5

59
 DateTime.Now.ToString("dd_MM_yyyy")
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 6
    be aware from the capital MM which is month and not mm which is minute .. very important ! – Royi Namir Oct 24 '11 at 10:39
  • 1
    This assumes the language does not use illegal filename characters in its dd, MM, or yyyy format. True for English. Not necessarily true for other languages. – Raymond Chen Oct 24 '11 at 12:33
  • @RoyiNamir Thanks, I have just tried using it and ran into that problem so came back to look at your answer, thanks! – Bali C Oct 24 '11 at 19:44
53

As written by Samich, but

// The following will produce 2011-10-24-13-10
DateTime.Now.ToString("yyyy-MM-dd-HH-mm", CultureInfo.InvariantCulture);

If you don't trust me, try setting the culture to new CultureInfo("ar-sa"); :-)

I'll add that, if you hate the world, this is the most precise thing to do:

DateTime.Now.Ticks.ToString("D19");

There are 19 digits in DateTime.MaxValue.Ticks

If you hate the world even more:

DateTime.Now.Ticks.ToString("X16");

There are 16 hex digits in DateTime.MaxValue.Ticks.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Wow, I didn't know there was this much detail to DateTime, +1, thanks! – Bali C Oct 24 '11 at 10:25
  • 3
    @BaliC I'll add that if you are in the "sport" of using `DateTime` to number your files, you should probably use `DateTime.UtcNow` so that you don't have problems around DST change time. – xanatos Oct 24 '11 at 10:28
11

Use a ToString pattern:

DateTime.Now.ToString("yyyy-MM-dd-HH-mm") // will produce 2011-10-24-13-10
ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
Samich
  • 29,157
  • 6
  • 68
  • 77
3

If you need the datetime to be more precise you can use:

DateTime.Now.ToString("O").Replace(":", "_") //2016-09-21T13_14_01.3624587+02_00

or (if you need a little less)

DateTime.Now.ToString("s").Replace(":", "_") //2016-09-21T13_16_11

Both are valid file names and are sortable.

You can create an extensions method:

public static string ToFileName(this DateTime @this)
{
    return @this.ToString("O").Replace(":", "_");
}

And then use it like this:

var saveZipToFile = "output_" + DateTime.Now.ToFileName() + ".zip";

The "s" format does not take time zones into account and thus the datetimes:

  • 2014-11-15T18:32:17+00:00
  • 2014-11-15T18:32:17+08:00

formatted using "s" are identical.

So if your datetimes represent dirrerent time zones I would recommend using "O" or using DateTime.UtcNow.

inwenis
  • 368
  • 7
  • 24
2

You can use timestamp's long format:

DateTime.Now.ToBinary()
BigMike
  • 6,683
  • 1
  • 23
  • 24