Datetime.Tryparse Century Control C#

DateTime.TryParse century control C#

It's tricky, because the way two digit years work with TryParse is based on the TwoDigitYearMax property of the Calendar property of the CultureInfo object that you are using. (CultureInfo->Calendar->TwoDigitYearMax)

In order to make two digit years have 20 prepended, you'll need to manually create a CultureInfo object which has a Calendar object with 2099 set as the TwoDigitYearMax property. Unfortunately, this means that any two digit date parsed will have 20 prepended (including 98, 99 etc.) which is probably not what you want.

I suspect that your best option is to use a 3rd party date parsing library instead of the standard tryparse that will use the +50/-50 year rule for 2 digit years. (that a 2 digit year should be translated into a range between 50 years before this year and 50 years greater than this year).

Alternatively, you could override the ToFourDigitYear method on the calendar object (it's virtual) and use that to implement the -50/+50 rule.

DateTime TryParse - mapping '99' to 2099, not 1999

Taken from this answer, you can supply ParseExact() with a culture object. Suspect TryParseExact() would be the same:

CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.LCID);
ci.Calendar.TwoDigitYearMax = 2099;
//Parse the date using our custom culture.
DateTime dt = DateTime.ParseExact(input, "MMM-yy", ci);

DateTime.ParseExact - different outputs on Windows 10 and WinServer2012R2

It's based on the culture's default calendar's TwoDigitYearMax property. If you change that property value, you'll get a different result:

using System;
using System.Globalization;

class Program
{
static void Main(string[] args)
{
var culture = CultureInfo.CurrentCulture;

// Clone returns a *mutable* copy.
culture = (CultureInfo) culture.Clone();

culture.DateTimeFormat.Calendar.TwoDigitYearMax = 2029;
var result = DateTime.ParseExact("Jan 30", "MMM yy", culture, System.Globalization.DateTimeStyles.None);
Console.WriteLine(result.Year); // 1930

culture.DateTimeFormat.Calendar.TwoDigitYearMax = 2129;
result = DateTime.ParseExact("Jan 30", "MMM yy", culture, System.Globalization.DateTimeStyles.None);
Console.WriteLine(result.Year); // 2030
}
}

what is the range of DateTime.TryParseExact or Convert.ToDateTime() having date format dd/MM/yy

If you're asking how a two digit year is interpreted, it depends on the culture's TwoDigitYearMax property

Which depends on the computer's settings. To see what the setting is go to:

  1. Control Panel -> Regional and Language Options
  2. Customize (XP) or Additional Settings (Win7)
  3. Date Tab. There is a setting there on how two digit years are interpreted. On my computer it's interpreted as between 1930 and 2029.

DateTime string parsing

Theoretically elegant way of doing this: change the TwoDigitYearMax property of the Calendar used by the DateTimeFormatInfo you're using to parse the text. For instance:

CultureInfo current = CultureInfo.CurrentCulture;
DateTimeFormatInfo dtfi = (DateTimeFormatInfo) current.DateTimeFormat.Clone();
// I'm not *sure* whether this is necessary
dtfi.Calendar = (Calendar) dtfi.Calendar.Clone();
dtfi.Calendar.TwoDigitYearMax = 1910;

Then use dtfi in your call to DateTime.ParseExact.

Practical way of doing this: add "20" to the start of your input, and parse with "yyyyMMdd".

How to convert MM/dd/yy string to a date with the nearest future year?

You can use this simpler approach:

string dateString = "02/11/48";

DateTimeOffset.TryParseExact(
dateString,
"MM/dd/yy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out DateTimeOffset date);

if (date < DateTimeOffset.UtcNow)
{
date = date.AddYears(100);
}

It simply adds 100 years if the parsed value is before now.

Creating a valid date from a MM/dd/yy format

You can change the Calendar.TwoDigitYearMax property to change the last year of the 100 year range.

DateTimeFormatInfo formatProvider = new DateTimeFormatInfo();
formatProvider.Calendar.TwoDigitYearMax = DateTime.Now.Year;

DateTime date = DateTime.ParseExact("12/12/22", "MM/dd/yy", formatProvider);

How to add a century prefix to a birth date?

Don't use Substring to parse a string value into a DateTime. .Net has very robust methods created for you to do this conversion.

Here I'm using DateTime.TryParseExact(), which lets me specify the exact format I expect dates values to be provided in. The method returns true or false indicating if the value is in that supplied format. No need to use exceptions to control logic flow.

public string GetCenturyPrefix(string socSecNo)
{
// Check if you're able to parse the incoming value
// in the format "yyMMdd".
if (!DateTime.TryParseExact(socSecNo, "yyMMdd", CultureInfo.InvariantCulture,
DateTimeStyles.None, out DateTime parsedDateTime))
{
// Do something if the input can't be parsed in that format.
// In this example I'm throwing an exception, but you can also
// return an empty string.
throw new Exception("Not valid date format");
}

// Extract only the Year portion as a 4 digit string,
// and return the first 2 characters.
return parsedDateTime.ToString("yyyy").Substring(0, 2);
}


Related Topics



Leave a reply



Submit