Formatting dates in .NET

When creating a dynamic folder structure from a date, escaping the backslash can be a bit tricky. For example,


will give a string like 2022 (easy), but what about the whole date?

Since the backslash is itself the escape character, escaping it will need four of them:


That’s because it will need to be escaped twice. Once in the string and then once more for the date time parser that parses the dateformat.