Home
C#
Filename With Date Example (DateTime.Now)
This page was last reviewed on Feb 1, 2024.
Dot Net Perls
Filename, DateTime. A file name can store the date and time. This clearly identifies the file in a unique way. There are many ways of accomplishing this.
Notes, file names. Some methods are clearer to read and easier to modify than others. We include the date, or the date and the time. A format string is provided.
DateTime
DateTime Format
File
An example. We use the current date and time as the name for a file on the file system. We call the string.Format method, and combine it with DateTime.Now.
string.Format
Result This method outputs the correct string based on the date and time. We use the string as a file name.
DateTime.Now
using System; using System.IO; class Program { static void Main() { // Write file containing the date with an extension. string n = string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bin", DateTime.Now); File.WriteAllText(n, "aaa"); } }
"text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bin" text- The first part of the output required Files will all start with text- {0: Indicates that this is a string placeholder The zero indicates the index of the parameters inserted here yyyy- Prints the year in four digits followed by a dash This has a "year 10000" problem MM- Prints the month in two digits dd_ Prints the day in two digits followed by an underscore hh- Prints the hour in two digits mm- Prints the minute, also in two digits ss- As expected, it prints the seconds tt Prints AM or PM depending on the time of day
Environment.SpecialFolder. You can use this code with Environment.SpecialFolder. You can encapsulate the logic for this in a property accessor.
Property
Part 1 This format string has 3 placeholders: the directory name we get from SpecialFolder, the separator char, and the DateTime.
Part 2 It gets the MyDocuments folder path. This line uses the SpecialFolder.MyDocuments enum value to get the path.
Environment
Part 3 The code uses Path.DirectorySeparatorChar. On Windows, this is the backslash—you could just use "\\" in the string instead.
Part 4 We get the current date and time by accessing the DateTime.Now property.
using System; using System.IO; class Program { static string SpecialFileName { get { // Part 1: specify the format string. return string.Format("{0}{1}text-{2:yyyy-MM-dd_hh-mm-ss-tt}.bin", // Part 2: call GetFolderPath. Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), // Part 3: specify separator char. Path.DirectorySeparatorChar, // Part 4: get current time. DateTime.Now); } } static void Main() { Console.WriteLine(SpecialFileName); } }
C:\Users\mount\OneDrive\Documents\text-2024-02-01_04-07-57-PM.bin
Notes, separators. You can change the string separators by replacing the underscores and dashes with any other character, excluding some special ones.
Note File names cannot have some characters. And some words are reserved on Windows—they cannot be file names.
A summary. We generated file names containing the current date. This is useful for "data dumps" and log files where daily files are written. It helps us organize output files.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 1, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.