get month and date on label in Format '7/07'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • parshupooja
    New Member
    • Jun 2007
    • 159

    get month and date on label in Format '7/07'

    Hello,

    Can anyone help in getting month and year in this format '12/07' or 6/07 on a label. I am using asp.net C#
    Please don't misunderstand the format its not mm/yy or 07/07. its just m/yy

    any help will be appreciated
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by parshupooja
    Hello,

    Can anyone help in getting month and year in this format '12/07' or 6/07 on a label. I am using asp.net C#
    Please don't misunderstand the format its not mm/yy or 07/07. its just m/yy

    any help will be appreciated
    You should be able to use the DateTime object to get the required data. The DateTime object has a Month and Year property. The Month property should print a single numeric month without a leading zero. If it does not just check to see if the Month starts with 0 then remove it using string.Replace or string.Substrin g. The Year property will return a 4 digit year. You will need to use string.Replace or string.Substrin g to get the last 2 digits of the year.

    Nathan

    Comment

    • cmrhema
      Contributor
      • Jan 2007
      • 375

      #3
      Originally posted by nateraaaa
      You should be able to use the DateTime object to get the required data. The DateTime object has a Month and Year property. The Month property should print a single numeric month without a leading zero. If it does not just check to see if the Month starts with 0 then remove it using string.Replace or string.Substrin g. The Year property will return a 4 digit year. You will need to use string.Replace or string.Substrin g to get the last 2 digits of the year.

      Nathan
      i have used current date and transfered it to text box
      you choose your relevant date and pass it to label
      textBox1.Text = DateTime.Now.To String("M/yy");

      Comment

      • parshupooja
        New Member
        • Jun 2007
        • 159

        #4
        Thank Youuuuuuuuuuuu,
        I got it

        Comment

        • nateraaaa
          Recognized Expert Contributor
          • May 2007
          • 664

          #5
          Originally posted by parshupooja
          Thank Youuuuuuuuuuuu,
          I got it
          What was the code that you used?

          Comment

          • parshupooja
            New Member
            • Jun 2007
            • 159

            #6
            sorry I should have mentioned a code I used
            Here it is
            Code:
            string mon = DateTime.Now.Month.ToString();
                    string year = DateTime.Now.Year.ToString();
                    string cyear = year.Substring(year.Length - 2, 2);
                    lblqtr.Text = mon + "/" + cyear;
            
                    string pmon = DateTime.Now.AddMonths(-1).Month.ToString();
                    string pyear = DateTime.Now.Year.ToString();
                    string Pyear = pyear.Substring(pyear.Length - 2, 2);
                    lblpqtr.Text = pmon + "/" + Pyear;

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              The
              Code:
              DateTime.Now.ToString("M/yy");
              would be a more simple solution, but what you got does work.

              Comment

              Working...