Solved

How do I convert to the proper Century in DateTime Format?

Posted on 2004-03-29
1
663 Views
Last Modified: 2008-03-03

I am validating a text file as input in my VB.NET module.  Trouble arises with six-digit date fields (ugh, the y2k beast is still around). I want to compare a Date of Birth field (from the 20th cent) with some other fairly current date value (from the 21st cent). But say I have a very old and distinguished person who has a DOB of 11/15/28 -- that's 11/15/1928. The below code coverts his DOB to 11/15/2028.   I can't find any documentation that tells me how to convert a date value back to the last century based on a two-digit year.
My Culture is neutral (although this is an American web app).

Code looks like this:

Dim dfi As System.Globalization.DateTimeFormatInfo = New System.Globalization.DateTimeFormatInfo
        dfi.ShortDatePattern = "MMddyy"
ElemValue = obts.Elem012   'DOB
ElemDateValue = DateTime.ParseExact(ElemValue, "d", dfi)
                If ElemDateValue > TodaysDate Then
                    DoRuleViolation("Birth date > than Today is not allowed")
                End If
0
Comment
Question by:bigsplash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
KarunSK earned 500 total points
ID: 10708901
You can set the TwoDigitYearMax property of the Calendar.

        dfi.Calendar.TwoDigitYearMax = 2027

This will cause all "yy" values that are greater than 27 to be interpreted as belonging to the previous century.

Karun.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question