Getting the century only from a year

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

    Getting the century only from a year

    Is there a format that can be used in DateTime.ToStri ng() that will
    allow you to get only the century of a given year. "yy" of course
    doesn't work, that returns the year itself, not the century.

  • Paul E Collins

    #2
    Re: Getting the century only from a year

    "Doug" <dnlwhite@dtgne t.com> wrote:
    [color=blue]
    > Is there a format that can be used in DateTime.ToStri ng()
    > that will allow you to get only the century of a given year.
    > "yy" of course doesn't work, that returns the year itself,
    > not the century.[/color]

    Well, get the full year that way, extract the first two digits of the
    year (e.g. "19" from "1984"), and add one (since 19xx is in the 20th
    century, for example).

    string firstTwoDigits = yearString.Subs tring(0, 2);
    int century = Convert.ToInt32 (firstTwoDigits ) + 1;

    You don't have to worry about years with fewer than two digits,
    because years before about 1750 aren't supported. (That's when the
    modern Gregorian calendar came into force.)

    Eq.


    Comment

    • Lars-Inge Tønnessen \(VJ# MVP\)

      #3
      Re: Getting the century only from a year

      It could be nice to have the century before the Gregorian calender too. :o)

      using System;
      using System.Collecti ons.Generic;
      using System.Text;

      namespace Century
      {
      class Program
      {

      public int Cent(int year)
      {
      if (year.ToString( ).Length == 1)
      return 1;
      else if (year.ToString( ).Length == 2)
      return 1;
      else if (year.ToString( ).Length == 3)
      return (Convert.ToInt3 2(year.ToString ().Substring(0, 1))) +
      1;
      else if (year.ToString( ).Length == 4)
      return (Convert.ToInt3 2(year.ToString ().Substring(0, 2))) +
      1;
      else
      return -1;
      }

      public Program()
      {
      System.Console. WriteLine("" + Cent(1));
      System.Console. WriteLine("" + Cent(13));
      System.Console. WriteLine("" + Cent(171));
      System.Console. WriteLine("" + Cent(1714));
      System.Console. WriteLine("" + Cent(2006));
      System.Console. ReadKey();
      }

      static void Main(string[] args)
      {
      new Program();
      }
      }
      }


      Regards,
      Lars-Inge Tønnessen


      Comment

      • Doug

        #4
        Re: Getting the century only from a year

        There's no way to use a format string of some kind? Like you can use
        MM for month and yy for year and so on, there's nothing to represent
        just the century?

        Comment

        • Paul E Collins

          #5
          Re: Getting the century only from a year

          "Doug" <dnlwhite@dtgne t.com> wrote:
          [color=blue]
          > There's no way to use a format string of some kind?
          > Like you can use MM for month and yy for year and
          > so on, there's nothing to represent just the century?[/color]

          That's correct. Look up "DateTimeFormat Info" in the Visual Studio help
          file, if you have it. That will show you the complete list, with no
          option for centuries.

          Eq.


          Comment

          • Michael Bray

            #6
            Re: Getting the century only from a year

            "Paul E Collins" <find_my_real_a ddress@CL4.org> wrote in news:dvpspf$6mo $1
            @nwrdmz03.dmz.n cs.ea.ibs-infra.bt.com:

            [color=blue]
            > Well, get the full year that way, extract the first two digits of the
            > year (e.g. "19" from "1984"), and add one (since 19xx is in the 20th
            > century, for example).
            >
            > string firstTwoDigits = yearString.Subs tring(0, 2);
            > int century = Convert.ToInt32 (firstTwoDigits ) + 1;[/color]

            Why not just (dtDateTimeVar. Year / 100) + 1 ?

            -mdb

            Comment

            Working...