1

I'm not sure in which version of C# they got added, but you can now use format specifiers to format DateTime objects like this:

var date = new DateTime(2021, 01, 01);
System.Console.WriteLine($"{date:MM.dd}"); // 01.01

Now I want to get the calendar week from a DateTime object. Since ISO8601 specifies a slightly different week numbering than .Net uses it, I need to provide custom logic to get the calendar week. The Problem I have now is not how to get the correct week numbers, since Microsoft provided a solution to that in their blog and there's also the ISOWeek class, but rather: How I can add a custom format specifier like above so that I can do something like this?

var date = new DateTime(2021, 01, 01);
System.Console.WriteLine($"{date:WWW}"); // 53-2020

Is this even possible in C# 9? I'm guessing not since I've found very little on the topic so far.

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
MrCodingB
  • 2,284
  • 1
  • 9
  • 22
  • 1
    Why not use an extension method? It's not as clean, but if custom format specifiers could be defined, that would lead to versioning problems. – Aluan Haddad May 19 '21 at 16:30
  • If you use at least .NET Core 3, then you can use [ISOWeek](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.isoweek). Otherwise, see [this](https://stackoverflow.com/q/11154673/5114784) post (duplicate?). – György Kőszeg May 19 '21 at 16:32
  • @GyörgyKőszeg this is not a duplicate, since my question is not how to get the right week number, but rather how I can implement a custom format specifier – MrCodingB May 19 '21 at 17:06
  • @AluanHaddad Yes that would be an option, and that's my current approach, but I just wanted to know if custom format specifiers are possible – MrCodingB May 19 '21 at 17:08
  • @MrCodingB: Since you cannot derive the `DateTime` struct you cannot add new format specifiers to it. But nothing stops you to create a `CustomDateTime` type (maybe with an implicit operator for `DateTime` conversion) so you can implement the [`IFormattable`](https://learn.microsoft.com/en-us/dotnet/api/system.iformattable) interface with any custom logic. See the link for an example. – György Kőszeg May 19 '21 at 17:44

1 Answers1

2

You can implement custom format specifiers with the letters and tokens you want for new types that you implement.

But you cannot change the acceptable format tokens for built-in types, like DateTime.

Take a look at this documentation.

Please note that what Microsoft calls Custom Format Specifiers, in this context, means combining the acceptable format tokens for DateTime as you wish, together with CultureInfo stuff.

You can check in the reference source how DateTime uses internally the class DateTimeFormat, that has a fixed set of tokens it accepts.

You best approach, I believe, is to write a method that gets the string and substitutes "WW" by ISOWeek.GetWeekOfYear(date)

Other options are:

  • Write an extension method for your convenience
  • Create a custom type implementing IFormattable with conversions to DateTime, overload operators and stuff... so much trouble, not worth it.

By the way, Custom DateTime Format Specifiers were introduced in c#6.

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
  • Ok, thanks! I suppose that would really be too much effort compared to a simple extension method. But thank you very much for the explanation. – MrCodingB May 19 '21 at 19:18