Skip to content

Icorrect comment for the year pattern in DateTimeFormat #95629

Closed

Description

@Guiorgy

Using more than 2 y characters in the format results in the year being formatted with leading zeroes, for example:

Console.WriteLine((new DateTime(23, 1, 2, 3, 4, 5, 6)).ToString("yyy")); // result: 023
Console.WriteLine(DateTime.Now.ToString("yyyyyy")); // result: 002023

This is correctly documented in learn.microsoft.com:

"yyy" The year, with a minimum of three digits. 0001-01-01T00:00:00 -> 001
More information: The "yyy" Custom Format Specifier. 0900-01-01T00:00:00 -> 900
1900-01-01T00:00:00 -> 1900
2009-06-15T13:45:30 -> 2009
"yyyy" The year as a four-digit number. 0001-01-01T00:00:00 -> 0001
More information: The "yyyy" Custom Format Specifier. 0900-01-01T00:00:00 -> 0900
1900-01-01T00:00:00 -> 1900
2009-06-15T13:45:30 -> 2009
"yyyyy" The year as a five-digit number. 0001-01-01T00:00:00 -> 00001
More information: The "yyyyy" Custom Format Specifier. 2009-06-15T13:45:30 -> 02009

However, the comments above DateTimeFormat and the 'y' case say otherwise:

"yyyyy" "D5" year 2000

and

// yyy/yyyy/yyyyy/... : Print year value. No leading zero.

It would be more correct to mention the leading zeroes, for example:

/*
"yyyyy" "D5"        year with leading zeroes               02000
...
*/

and

// yyy/yyyy/yyyyy/... : Print year value.  With leading zeroes.

Activity

  1. ghost added
    untriaged
    on Dec 5, 2023
  2. added this to the Future milestone on Dec 5, 2023
  3. ghost removed
    untriaged
    on Dec 5, 2023
  4. added
    help wanted
    untriaged
    and removed
    untriaged
    on Dec 5, 2023
  5. tarekgh commented on Dec 5, 2023

    @tarekgh
    Member

    @Guiorgy are you interested to submit a PR to fix this?

  6. MichalPetryka commented on Dec 5, 2023

    @MichalPetryka
    Contributor

    are you interested to submit a PR to fix this?

    Would this be a documentation change or behaviour change?

  7. Guiorgy commented on Dec 5, 2023

    @Guiorgy
    ContributorAuthor

    @MichalPetryka Unless we want to change the API, only the comments need to be edited to reflect the reality, so I guess documentation?

  8. Guiorgy commented on Dec 5, 2023

    @Guiorgy
    ContributorAuthor

    @tarekgh Is #95650 satisfactory?

  9. Guiorgy commented on Dec 5, 2023

    @Guiorgy
    ContributorAuthor

    Actually, "yyyy" also prints with leading zeroes when year is below 1000:'

    var date = new DateTime(23, 1, 2, 3, 4, 5, 6);
    
    Console.WriteLine(date.ToString("y")); // January 0023
    Console.WriteLine(date.ToString("yyy")); // 023

    So I don't thing the PR is quite right
    Changed commit

  10. tarekgh commented on Dec 5, 2023

    @tarekgh
    Member

    Right, all yy, yyy, yyyy, and yyyyy are using leading zeros if the year number has number of digits less than the number of y's.

  11. ghost added
    in-pr
    on Dec 5, 2023
  12. ghost removed
    in-pr
    on Dec 6, 2023
  13. locked and limited conversation to collaborators on Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Globalizationhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions