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.
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