gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
how to get date and time in a filename c#
- User
how to get date and time in a filename c#
- Assistant
In C#, you can easily get the current date and time to include in a filename by using the
DateTimeclass. You typically want to format the date and time in a way that is safe for filenames (i.e., avoiding characters that are not allowed in filenames, such as slashes and colons).Here’s an example of how to create a filename with the current date and time formatted appropriately:
csharpusing System; class Program { static void Main() { // Get current date and time DateTime now = DateTime.Now; // Format the date and time to be safe for filenames string formattedDate = now.ToString("yyyyMMdd_HHmmss"); // Create a filename string filename = $"File_{formattedDate}.txt"; // Output the filename Console.WriteLine("Generated filename: " + filename); } }