Avatar ·

How to convert DateTime to string in format "YYYYMMDDHHMMSS" in C#?

Hi everyone! I'm new to C# and struggling with what seems like a simple task. I need to save the current date and time as a string in the format "YYYYMMDDHHMMSS" — that is, without separators, as a continuous number. For example, for March 5, 2025, 14:30:45, the result should be "20250305143045". \n\nI know that C# has a `DateTime.ToString()` method, but when I write:\n\n```csharp\nDateTime now = DateTime.Now;\nstring result = now.ToString("YYYYMMDDHHMMSS");\n```\n\nI don't get what I expect. Instead of "20250305143045", it outputs something like "YYYYMMDDHHMMSS" — that is, my letters are just copied into the string. I tried writing in lowercase, but it doesn't work either.\n\nI Googled it, but found a bunch of different options with separators (dashes, colons), and I need exactly a compact format without them. Please tell me how to