Date time format (Uppercase)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arjen

    Date time format (Uppercase)

    Hello,

    How can I make the first letter uppercase?

    ToString("ddd. dd MMMM yyyy")
    This ddd. will be for example monday, I want to see Monday.

    Thanks!



  • Jon Skeet [C# MVP]

    #2
    Re: Date time format (Uppercase)

    Arjen <boah123@hotmai l.com> wrote:[color=blue]
    > How can I make the first letter uppercase?
    >
    > ToString("ddd. dd MMMM yyyy")
    > This ddd. will be for example monday, I want to see Monday.
    >
    > Thanks![/color]

    That's odd - the above gives "Mon" for me - I need "dddd" to get the
    full day name. Either way, it begins with a capital letter. What
    culture are you in?

    You can always do post-processing, of course, and capitalise the first
    letter that way...

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Arjen

      #3
      Re: Date time format (Uppercase)

      Whoops, I made a mistake. Sorry.

      I see now "mon." but I want to see "Mon.".
      Is that posible?

      Thanks!




      "Jon Skeet [C# MVP]" <skeet@pobox.co m> schreef in bericht
      news:MPG.1af0ca 115dc822b798a71 7@msnews.micros oft.com...[color=blue]
      > Arjen <boah123@hotmai l.com> wrote:[color=green]
      > > How can I make the first letter uppercase?
      > >
      > > ToString("ddd. dd MMMM yyyy")
      > > This ddd. will be for example monday, I want to see Monday.
      > >
      > > Thanks![/color]
      >
      > That's odd - the above gives "Mon" for me - I need "dddd" to get the
      > full day name. Either way, it begins with a capital letter. What
      > culture are you in?
      >
      > You can always do post-processing, of course, and capitalise the first
      > letter that way...
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Date time format (Uppercase)

        Arjen <boah123@hotmai l.com> wrote:[color=blue]
        > Whoops, I made a mistake. Sorry.
        >
        > I see now "mon." but I want to see "Mon.".
        > Is that posible?[/color]

        I believe that to do that, you would either need to do tricks with
        CultureInfos, or (easier) just lowercase the first letter manually:

        string original = date.ToString(" ...");
        string lowered = Char.ToLower(or iginal[0])+original.Subs tring(1);

        Not the fastest code in the world, but it'll work and it's simple.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Bruno Jouhier [MVP]

          #5
          Re: Date time format (Uppercase)

          I'm afraid that you don't have control over this and that you have to do
          some string manipulations if you want to control it.

          On the other hand, you should not try to control capitalization yourself
          because it is culture dependent. For example, the names of the days are
          capitalized in English (Monday, Tuesday, ...) but not in French (lundi,
          mardi, ...). So, you will do the wrong thing if you capitalize them without
          checking the culture.

          Also, ddd will display "Mon" (capitalized), and dddd will produce "Monday"
          in the usual English cultures. How did you get the "monday" result?

          Bruno.

          "Arjen" <boah123@hotmai l.com> a écrit dans le message de
          news:c66b7o$qon $1@news1.tilbu1 .nb.home.nl...[color=blue]
          > Hello,
          >
          > How can I make the first letter uppercase?
          >
          > ToString("ddd. dd MMMM yyyy")
          > This ddd. will be for example monday, I want to see Monday.
          >
          > Thanks!
          >
          >
          >[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Date time format (Uppercase)

            Bruno Jouhier [MVP] <bjouhier@clu b-internet.fr> wrote:[color=blue]
            > I'm afraid that you don't have control over this and that you have to do
            > some string manipulations if you want to control it.[/color]

            Actually, I've just come up with a slightly nicer way of doing it - you
            only need to do the string manipulations once.

            Basically, you clone an existing DateTimeFormatI nfo and alter the
            DayNames and AbbreviatedDayN ames properties, and then pass that
            DateTimeFormatI nfo in when formatting the DateTime:

            using System;
            using System.Globaliz ation;

            class Test
            {
            static void Main()
            {
            DateTimeFormatI nfo dtfi =
            CultureInfo.Cur rentCulture.Dat eTimeFormat;

            dtfi = (DateTimeFormat Info) dtfi.Clone();

            dtfi.DayNames = LowerNames(dtfi .DayNames);
            dtfi.Abbreviate dDayNames = LowerNames
            (dtfi.Abbreviat edDayNames);

            Console.WriteLi ne (DateTime.Now.T oString("ddd. dd", dtfi));
            }

            static string[] LowerNames (string[] old)
            {
            string[] ret = new string[old.Length];

            for (int i=0; i < ret.Length; i++)
            {
            ret[i] = Char.ToLower(ol d[i][0])+old[i].Substring(1);
            }
            return ret;
            }
            }

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...