I'm trying to parse birthdays in the format of d/M without any year specified.
Using DateTime.TryParseExact(birthday, "d/M", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime birthdayDate) works most of the time, except when the birthday is at leap year day (aka 29/2), and the parsing never succeeds because it is default to current year. Using DatTimeStyles.NoCurrentDateDefault does not work either since it defaults to year = 1, which is not a leap year.
How do I do the parsing so it does not involve a hack (I don't want to parse it manually or manually add an arbitrary year to parse it as a full date either, they are all ugly and potentially fragile) and still work across all possible dates, including leap day? Existing questions does not help at all since nobody bothers to check if they work for leap day at all. I tried all of them and none of them worked.
A day and a month does not make a date - therefore they can't be parsed as a DateTime value without assuming a year. The .NET Framework assumes a year that is either the current year or year 1 - exactly because 2/29 is valid only on leap years - and this is a very reasonable assumption.
The .Net framework does not provide a built in way to store Day/Month values, but Noda Time does - Take a look at AnnualDate - It stores a day and a month but no year.
However, it doesn't have Parse or TryParse methods - so for that you still need to manually manipulate the input string and add a year (that is a leap year like 2016) in order to use the DateTime's TryParseExact method.
Update
As Matt Johnson wrote in his comment, Noda Time does provide a way for parsing text as an AnnualDate, using the AnnualDatePattern class.
The documentation has a page called Patterns for AnnualDate values that lists the supported patterns.
This question lacks the appropriate details, however
If you just want to parse a birth date and month including leap year dates, then just added a leap year to the end of the date.
I am not sure what you expect to do here, however you could try this
birthday = $"{birthday}/2016"; // leap year
DateTime.TryParseExact(birthday, "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime birthdayDate);
Update
my question is how to not make TryParseExact assume the year
automatically by manually overriding it in some way
To be technical here, no you have to specify the leap year in the string if you are parsing a leap year month and day exclusively as in your example
There is a lot of checks and balances the TryParseExact method does, however here are the important bits.
In short, it uses the current year or year 1, and there is no way to tell it to choose a leap year specifically
private static bool CheckDefaultDateTime(ref DateTimeResult result, ref Calendar cal, DateTimeStyles styles)
{
if ((result.flags & ParseFlags.CaptureOffset) != (ParseFlags) 0 && (result.Month != -1 || result.Day != -1) && ((result.Year == -1 || (result.flags & ParseFlags.YearDefault) != (ParseFlags) 0) && (result.flags & ParseFlags.TimeZoneUsed) != (ParseFlags) 0))
{
result.SetFailure(ParseFailureKind.Format, "Format_MissingIncompleteDate", (object) null);
return false;
}
if (result.Year == -1 || result.Month == -1 || result.Day == -1)
{
DateTime dateTimeNow = DateTimeParse.GetDateTimeNow(ref result, ref styles);
if (result.Month == -1 && result.Day == -1)
{
if (result.Year == -1)
{
if ((styles & DateTimeStyles.NoCurrentDateDefault) != DateTimeStyles.None)
{
cal = GregorianCalendar.GetDefaultInstance();
result.Year = result.Month = result.Day = 1;
}
else
{
result.Year = cal.GetYear(dateTimeNow);
result.Month = cal.GetMonth(dateTimeNow);
result.Day = cal.GetDayOfMonth(dateTimeNow);
}
}
else
{
result.Month = 1;
result.Day = 1;
}
}
else
{
if (result.Year == -1)
result.Year = cal.GetYear(dateTimeNow);
if (result.Month == -1)
result.Month = 1;
if (result.Day == -1)
result.Day = 1;
}
}
if (result.Hour == -1)
result.Hour = 0;
if (result.Minute == -1)
result.Minute = 0;
if (result.Second == -1)
result.Second = 0;
if (result.era == -1)
result.era = 0;
return true;
}
This is for an assignment. I need to create a program for a sandwich shop. Part of it is to validate a user's payment info. The guidelines for this assignment are
Credit card number must be 16 digits; the first 4 digits must be one of these:
1298, 1267, 4512, 4567, 8901, 8933
Expiration date must be 2 digits for the month, 4 digits for the year. The month must be 01 - 12. The year must begin with "20". The date must be greater than the current actual month and year, and less than 6 years from the current actual month and year.
I have a text box for each number, txtCardNumber, txtSecuritycode, and txtExpiration. What I have works, but I think it's kind of messy. What would be a better way? And would it be possible to have only one Validate() method instead of a separate one for each?
public bool IsValidCard()
{
if (txtCardNumber.Text.StartsWith("1298") ||
txtCardNumber.Text.StartsWith("1267") ||
txtCardNumber.Text.StartsWith("4512") ||
txtCardNumber.Text.StartsWith("4567") ||
txtCardNumber.Text.StartsWith("8901") ||
txtCardNumber.Text.StartsWith("8933"))
{
if (Regex.Replace(txtCardNumber.Text, #"\s+", "").Length == 16)
{
return true;
}
}
return false;
}//end IsValidCard()
public bool IsValidSecurityCode()
{
bool isValid = Regex.Match(txtSecurityCode.Text, #"^\d{3}$").Success;
return isValid;
}//end IsValidSecurityCode()
public bool IsValidExpiration()
{
string[] date = Regex.Split(txtExpiration.Text, "/");
string[] currentDate = Regex.Split(DateTime.Now.ToString("MM/yyyy"), "/");
int compareYears = string.Compare(date[1], currentDate[1]);
int compareMonths = string.Compare(date[0], currentDate[0]);
//if expiration date is in MM/YYYY format
if (Regex.Match(txtExpiration.Text, #"^\d{2}/\d{4}$").Success)
{
//if month is "01-12" and year starts with "20"
if (Regex.Match(date[0], #"^[0][1-9]|[1][0-2]$").Success)
{
//if expiration date is after current date
if ((compareYears == 1) || (compareYears == 0 && (compareMonths == 1)))
{
return true;
}
}
}
return false;
}//end IsValidExpiration
Note: I haven't worked on validating the six year requirement.
You can use the following method, which will validate all the credit card info as per your requirements.
<1> Credit card number must be 16 digits; the first 4 digits must be one of these: 1298, 1267, 4512, 4567, 8901, 8933
<2> Security code must be 3 digit numeric (assumed)
<3> Expiration date must be 2 digits for the month, <4> 4 digits for the year. <5> The month must be 01 - 12. <6> The year must begin with "20". <7> The date must be greater than the current actual month and year, and <8> less than 6 years from the current actual month and year.
public static bool IsCreditCardInfoValid(string cardNo, string expiryDate, string cvv)
{
var cardCheck = new Regex(#"^(1298|1267|4512|4567|8901|8933)([\-\s]?[0-9]{4}){3}$");
var monthCheck = new Regex(#"^(0[1-9]|1[0-2])$");
var yearCheck = new Regex(#"^20[0-9]{2}$");
var cvvCheck = new Regex(#"^\d{3}$");
if (!cardCheck.IsMatch(cardNo)) // <1>check card number is valid
return false;
if (!cvvCheck.IsMatch(cvv)) // <2>check cvv is valid as "999"
return false;
var dateParts = expiryDate.Split('/'); //expiry date in from MM/yyyy
if (!monthCheck.IsMatch(dateParts[0]) || !yearCheck.IsMatch(dateParts[1])) // <3 - 6>
return false; // ^ check date format is valid as "MM/yyyy"
var year = int.Parse(dateParts[1]);
var month = int.Parse(dateParts[0]);
var lastDateOfExpiryMonth = DateTime.DaysInMonth(year, month); //get actual expiry date
var cardExpiry = new DateTime(year, month, lastDateOfExpiryMonth, 23, 59, 59);
//check expiry greater than today & within next 6 years <7, 8>>
return (cardExpiry > DateTime.Now && cardExpiry < DateTime.Now.AddYears(6));
}
Though this works fine for all the conditions, please check properly before using in production.
This question already has answers here:
Wanted: DateTime.TryNew(year, month, day) or DateTime.IsValidDate(year, month, day)
(2 answers)
Closed 4 years ago.
I am creating date using following code
try
{
newdatetime = new DateTime(2012, 2, 30);
break;
}
catch (ArgumentOutOfRangeException)
{
// Try 29 Feb if not 28.
}
The catch block is to catch the invalid date like 30 Feb. Is there any way to verify if the date is valid by speciying the parameters like (year, month, day)?
Well, with months you know the valid range so you can constrain that manually. Years are obviously not constrained in the normal sense, but are instead limited by the amount that DateTime can actually hold (0001 to 9999).
With days, there is the DaysInMonth(int year, int month) method that can tell you the maximum days for the provided month. This also gives you the leap year.
With this information, you can create your own method to check the range based on the provided integers.
Something like:
public static bool AreValidDateValues(int year, int month, int day)
{
if (month < 1 || month > 12)
return false;
if (year < DateTime.MinValue.Year || year > DateTime.MaxValue.Year)
return false;
var days = DateTime.DaysInMonth(year, month);
if (day < 1 || day > days)
return false;
return true;
}
Or if you can't be bothered with that, convert the raw values into a string representation of a date and put that into DateTime.TryParse, which will give a true/false for the provided string - just be careful with culture-sensitive parsing.
You can use DateTime.TryParse to perform the check :
DateTime d;
var isValid = DateTime.TryParse(String.Format("{0}/{1}/{2}", 2, 31, 2012), out d);
Console.WriteLine(isValid);
This is a question of best practices. I have a utility that takes in a two digit year as a string and I need to convert it to a four digit year as a string. right now I do
//DOB's format is "MMM (D)D YY" that first digit of the day is not there for numbers 1-9
string tmpYear = rowIn.DOB.Substring(rowIn.DOB.Length - 3, 2); //-3 because it is 0 indexed
if (Convert.ToInt16(tmpYear) > 50)
tmpYear = String.Format("19{0}", tmpYear);
else
tmpYear = String.Format("20{0}", tmpYear);
I am sure I am doing it horribly wrong, any pointers?
The .NET framework has a method that does exactly what you want:
int fourDigitYear = CultureInfo.CurrentCulture.Calendar.ToFourDigitYear(twoDigitYear)
That way you will correctly adhere to current regional settings as defined in Control Panel (or group policy):
Given that there are people alive now born before 1950, but none born after 2010, your use of 50 as the flipping point seems broken.
For date of birth, can you not set the flip point to the 'year of now' (i.e. 10) in your app? Even then you'll have problems with those born before 1911...
There's no perfect way to do this - you're creating information out of thin air.
I've assumed DOB = date-of-birth. For other data (say, maturity of a financial instrument) the choice might be different, but just as imperfect.
You can also use the DateTime.TryParse method to convert your date. It uses the current culture settings to define the pivot year (in my case it is 2029)
DateTime resultDate;
Console.WriteLine("CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax : {0}", System.Globalization.CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax);
DateTime.TryParse("01/01/28", out resultDate);
Console.WriteLine("Generated date with year=28 - {0}",resultDate);
DateTime.TryParse("01/02/29",out resultDate);
Console.WriteLine("Generated date with year=29 - {0}", resultDate);
DateTime.TryParse("01/03/30", out resultDate);
Console.WriteLine("Generated date with year=30 - {0}", resultDate);
The output is:
CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax : 2029
Generated date with year=28 - 01/01/2028 00:00:00
Generated date with year=29 - 01/02/2029 00:00:00
Generated date with year=30 - 01/03/1930 00:00:00
If you want to change the behavior you can create a culture with the year you want to use as pivot. This thread shows an example
DateTime.TryParse century control C#
But as martin stated, if you want to manage a time period that spans more than 100 year, there is no way to do it with only 2 digits.
I think Java has a good implementation of this:
http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html#year
People rarely specify years far into the future using a two-digit code. The Java implementation handles this by assuming a range of 80 years behind and 20 years ahead of the current year. So right now, 30 would be 2030, while 31 would be 1931. Additionally, this implementation is flexible, modifying its ranges as time goes on, so that you don't have to change the code every decade or so.
I just tested, and Excel also uses these same rules for 2-digit year conversion. 1/1/29 turns into 1/1/2029. 1/1/30 turns into 1/1/1930.
The implementation of
System.Globalization.CultureInfo.CurrentCulture.Calendar.ToFourDigitYear
is
public virtual int ToFourDigitYear(int year)
{
if (year < 0)
throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (year < 100)
return (this.TwoDigitYearMax / 100 - (year > this.TwoDigitYearMax % 100 ? 1 : 0)) * 100 + year;
else
return year;
}
Hope this helps!
It might be smarter to check tmpYear > currentYear%100. If it is, then it's 19XX, otherwise 20XX.
This solution we use for Expiration Dates, the user enters MM and YY into separate fields. This results in dates being the 31st or 30th and 28th or 29th also for February.
/// <summary>
/// Creates datetime for current century and sets days to end of month
/// </summary>
/// <param name="MM"></param>
/// <param name="YY"></param>
/// <returns></returns>
public static DateTime GetEndOfMonth(string MM, string YY)
{
// YY -> YYYY #RipVanWinkle
// Gets Current century and adds YY to it.
// Minus 5 to allow dates that may be expired to be entered.
// eg. today is 2017, 12 = 2012 and 11 = 2111
int currentYear = DateTime.Now.Year;
string thisYear = currentYear.ToString().Substring(0, 2) + YY;
int month = Int32.Parse(MM);
int year = Int32.Parse(thisYear);
if ((currentYear - 5) > year)
year += 100;
return new DateTime(year, month, DateTime.DaysInMonth(year, month));
}
This Method can convert the credit card last two year digits to four year
private static int ToFourDigitYear(int year)
{
string stringYear = year.ToString("00");
if (stringYear.Length == 2)
{
int currentYear = DateTime.Now.Year;
string firstTwoDigitsOfCurrentYear = currentYear.ToString().Substring(0, 2);
year = Convert.ToInt32(firstTwoDigitsOfCurrentYear + stringYear);
if (year < currentYear)
year = year + 100;
}
return year;
}
Out of curiosity, from where do you get this data? From a form? In that case; I would simply ask the user to fill in (or somehow select) the year with four digits or get the users age and month/day of birth, and use that data to figure out what year they were born. That way, you wouldn't have to worry about this problem at all :)
Edit: Use DateTime for working with this kind of data.
Try this simple code
//Invoke TextBoxDateFormat method with date as parameter.
Method
public void TextBoxDateFormat(string str1)
{
// Takes the current date format if MM/DD/YY or MM/DD/YYYY
DateTime dt = Convert.ToDateTime(str1);
//Converts the requested date into MM/DD/YYYY and assign it to textbox field
TextBox = String.Format("{0:MM/dd/yyyy}", dt.ToShortDateString());
//include your validation code if required
}
Had a similar issue, and came up with this... HTH!
value = this.GetDate()
if (value.Length >= 6)//ensure that the date is mmddyy
{
int year = 0;
if (int.TryParse(value.Substring(4, 2), out year))
{
int pastMillenium = int.Parse(DateTime.Now.ToString("yyyy").Substring(0, 2)) - 1;
if (year > int.Parse(DateTime.Now.ToString("yy")))//if its a future year it's most likely 19XX
{
value = string.Format("{0}{1}{2}", value.Substring(0, 4), pastMillenium, year.ToString().PadLeft(2, '0'));
}
else
{
value = string.Format("{0}{1}{2}", value.Substring(0, 4), pastMillenium + 1, year.ToString().PadLeft(2, '0'));
}
}
else
{
value = string.Empty;
}
}
else
{
value = string.Empty;
}
My answer will not match your question but for credit cards I just add 2 digits of current year
private int UpconvertTwoDigitYearToFour(int yearTwoOrFour)
{
try
{
if (yearTwoOrFour.ToString().Length <= 2)
{
DateTime yearOnly = DateTime.ParseExact(yearTwoOrFour.ToString("D2"), "yy", null);
return yearOnly.Year;
}
}
catch
{
}
return yearTwoOrFour;
}
If you calculate for a person he will probably not be more than 100 years...
Eg: 751212
var nr = "751212";
var century = DateTime.Now.AddYears(-100).Year.ToString().Substring(0, 2);
var days = (DateTime.Now - DateTime.Parse(century + nr)).Days;
decimal years = days / 365.25m;
if(years>=99)
century = DateTime.Now.Year.ToString().Substring(0, 2);
var fullnr = century+nr;
To change a 2-digit year to 4-digit current or earlier -
year = year + (DateTime.Today.Year - DateTime.Today.Year%100);
if (year > DateTime.Today.Year)
year = year - 100;
My two cents,
Given an age range=[18, 100+], two digits year=90, I can do
current year - twoDigitsYear = 2018 - 90 = 1928, I got 19, 28
hence 19 is the first two digits of year of born, and 28 is the age, which is
year=1990, age=28
But it won't work when age 0 and 100 both included in the range, same to some of the other answers here.
Based on above solutions, here is mine, i used in android while using java
it takes current year in two digit format then checks for if input
year length is equal to 2, if yes then it get current year and from
this year it splits first two digits of century, then it adds this
century with year user input. to make it 4 digit year.
public static int getConvertedYearFromTwoToFourDigits(String year) {
if (year.length() == 2) {
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
String firstTwoDigitsOfCurrentYear = String.valueOf(currentYear).substring(0, 2);
year = firstTwoDigitsOfCurrentYear + year;
}
return Integer.parseInt(year);
}
int fYear = Convert.ToInt32(txtYear.Value.ToString().Substring(2, 2));
My answer will not match your question but for credit cards I just add 2 digits of current year
private int UpconvertTwoDigitYearToFour(int yearTwoOrFour)
{
try
{
if (yearTwoOrFour.ToString().Length <= 2)
{
DateTime yearOnly = DateTime.ParseExact(yearTwoOrFour.ToString("D2"), "yy", null);
return yearOnly.Year;
}
}
catch
{
}
return yearTwoOrFour;
}