Hello All

Here is my problem :

I have 3 variables
1. month
2. year
3. day

all three of those variables have numeric values for example

month = 04
year = 89
day = 17

I want to display it like this : 17 Apr 1989

Here is what I've tried

DateTime date = new DateTime( year, month, day );

lblDate.Text = String.Format( "{0}", date.ToString( "dd MMM yyyy" ) );

Output is :

17 Apr 0089

How can I correct the years so that it is 1989?

Thank you

:)

Add 1900 to the year.

DateTime date = new DateTime(1900+year, month, day);

Add 1900 to the year.

DateTime date = new DateTime(1900+year, month, day);

Hi there

Thank you for your reply

I did think of that. But then there is a problem when I want to say : example

year = 09
month = 01
day = 01

then the ouput is as follows :

01 Jan 1909

where it should be :

01 Jan 2009

Be aware that 2 digit dates *will* cause overlap between the 1900s and the 2000s, but if you have a hard lower bound on the 1900s, like 1970, you can check for that explicitly and be good until the same year in the 2000s:

// Works until 2070
if (year < 70)
    year += 2000;
else
    year += 1900;

There's not a good solution other than using 4 digit dates everywhere.

How would you figure out what year 89 stands for?
89 BC? 1689? 3089?
Well the method that does the string conversion can't either.
So the best the method can come up with is is to use two zeros. Hence 0089 if you initialise the year of a date with 89.

How would you figure out what year 89 stands for?
89 BC? 1689? 3089?
Well the method that does the string conversion can't either.
So the best the method can come up with is is to use two zeros. Hence 0089 if you initialise the year of a date with 89.

In the assignment i recieved it says the following :


You have to enter your birthdate : like this...

900213

into a text box and when the button is clicked the output must be

13 Feb 1990

You could do something nasty like this:

using System;

namespace DW_412456_CS_CON
{
   class Program
   {
      public static string GetAssumedDate(string strBirthDate)
      {
         int intBirthYear = int.Parse(strBirthDate.Substring(0, 2));
         int intBirthMonth = int.Parse(strBirthDate.Substring(2, 2));
         int intBirthDay = int.Parse(strBirthDate.Substring(4, 2));
         string strNowYear = DateTime.Now.Year.ToString();
         int intNowCentury = int.Parse(strNowYear.Substring(0, 2));
         int intNowDecade = int.Parse(strNowYear.Substring(2, 2));
         int intAssumedYear = ((intNowCentury - ((intNowDecade < intBirthYear) ? 1 : 0)) * 100) + intBirthYear; // NASTY
         return new DateTime(intAssumedYear, intBirthMonth, intBirthDay).ToString("dd MMM yyyy");
      }

      static void Main(string[] args)
      {
         Console.WriteLine(GetAssumedDate("900213"));
         Console.WriteLine(GetAssumedDate("030213"));
      }
   }
}

Explanation: That assumes if the birth decade is greater than this year's decade, then the birthdate had to be last century. Because birthdays (birth dates) are involved, people must have been born to have one :)

This will eventually fail for people over 100.

Explanation: That assumes if the birth decade is greater than this year's decade, then the birthdate had to be last century. Because birthdays (birth dates) are involved, people must have been born to have one :)

This will eventually fail for people over 100.

Hey

Thank you your solution was essentially the best. I understood what you wanted to do hehe but thanx for the explanation anyways.

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.