Solved

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

Posted on 2004-03-29
1
652 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now