[RESOLVED]how to chek if 3 data taken form a form are a valid Date

Hi, i got 3 dropdownlist with DAY, MONTH and YEAR

 @Html.DropDownList("DateOfBirthDay", Enumerable.Range(1, 31).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString() }), "-- Select Day --")
                @Html.DropDownList("DateOfBirthMonth", Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i) }), "-- Select Month --")
                @Html.DropDownList("DateOfBirthYear", Enumerable.Range(1900, 109).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString() }), "-- Select Year --")
                

Each one have a numerical value,  what i would love to do is to check if putting them together i got a real date

example

var mynewdate = DateOfBirthDay + "/" + DateOfBirthMonth "/" + DateOfBirthYear

how to chek if this will be a real date? (example, if i put 31/02/2014 it is not a real date)

and after this, how to convert this var to be a date?

Thanks

Hi, u done this code, in my controller

// POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(string DateOfBirthDay , string DatehOfBirthMonth, string DateOfBirthYear, RegisterViewModel model)

                DateTime fromDateValue;
                string FullDateOfBirth = DateOfBirthYear + "-" + DatehOfBirthMonth + "-" + DateOfBirthDay;
                string FullDateOfBirthLast = DateOfBirthYear + "-" + DatehOfBirthMonth + "-28";
                var formats = new[] { "yyyy-MM-dd" };
                if (DateTime.TryParseExact(FullDateOfBirth, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out fromDateValue))
                {
                    // do for valid date
                    DateTime FullDateOfBirthFormatDate = Convert.ToDateTime(FullDateOfBirth);
                    user.DateOfBirth = FullDateOfBirthFormatDate;
                }
                else
                {
                    // do for invalid date
                    DateTime FullDateOfBirthLastFormatDate = Convert.ToDateTime(FullDateOfBirthLast);
                    user.DateOfBirth = FullDateOfBirthLastFormatDate;

                }

but i get error

La chaîne n'a pas été reconnue en tant que DateTime valide.

Description : Une exception non gérée s'est produite au moment de l'exécution de la requête Web actuelle. Contrôlez la trace de la pile pour plus d'informations sur l'erreur et son origine dans le code. 

Détails de l'exception: System.FormatException: La chaîne n'a pas été reconnue en tant que DateTime valide.

Erreur source: 


Ligne 820 :                {
Ligne 821 :                    // do for invalid date
Ligne 822 :                    DateTime FullDateOfBirthLastFormatDate = Convert.ToDateTime(FullDateOfBirthLast);
Ligne 823 :                    user.DateOfBirth = FullDateOfBirthLastFormatDate;
Ligne 824 :

Fichier source : c:FRANCESCOProjectHairCollection3HairCollection3ControllersAccountController.cs    Ligne : 822 

Trace de la pile: 

why? what is worng?

Hi,

If possible make your datetime property nullable -> datetime ? to accept the null value and do the null check in your controller.

Hi grafic,

Please modify your code like this:

DateTime fromDateValue;
                string FullDateOfBirth = DateOfBirthYear + "-" + DatehOfBirthMonth + "-" + DateOfBirthDay;
                string FullDateOfBirthLast = DateOfBirthYear + "-" + DatehOfBirthMonth + "-28";
                var formats = new[] { "yyyy-MM-dd" };
                if (DateTime.TryParseExact(FullDateOfBirth, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out fromDateValue))
                {
                    user.DateOfBirth = fromDateValue;
                }

Best Regards

Starain Chen

Hi
Starain chen…
,

i did two variables : FullDateOfBirth
and  FullDateOfBirthLast
because i wanna check if FullDateOfBirth 
is a date and in that case i would love to save it in my filed
DateOfBirth
, but if is not a date i thoght that i could do a default date which contains the year, the month, and after the day 28, because it always exist a month with 28 days…

so in case that my var FullDateOfBirth
it is not a date, i would love to save the other one FullDateOfBirthLast 
in my field DateOfBirth..

How to do?

Hi grafic,

Base on the error message, it means that that value can’t convert to datetime. So you need check the value of
FullDateOfBirthLast.

Best Regards

Starain Chen

Hi
Starain chen…
, i updated with your code but it skips and it looks like the date is not a valid date, in fact ias a date of birth i found the date of today…

This is the code i done :

//Set the date of birth
                DateTime fromDateValue;
                string FullDateOfBirth = user.DateOfBirthYear + "-" + user.DateOfBirthMonth + "-" + user.DateOfBirthDay;
                string FullDateOfBirthLast = user.DateOfBirthYear + "-" + user.DateOfBirthMonth + "-28";
                var formats = new[] { "yyyy-MM-dd" };
                if (DateTime.TryParseExact(FullDateOfBirth, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out fromDateValue))
                {
                    user.DateOfBirth = fromDateValue;
                }
                else
                {
                    DateTime fromDateValueLast;
                    if (DateTime.TryParseExact(FullDateOfBirthLast, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out fromDateValueLast))
                    {
                        user.DateOfBirth = fromDateValueLast;
                    }
                    else
                    {
                        user.DateOfBirth = DateTime.Now;
                    }
                }

i done the debug and the for the code the 2 dates ( FullDateOfBirth , FullDateOfBirthLast ) are not a valid dates …

when i done the debug i got this values :

FullDateOfBirth = "1914-9-6"

fromDateValue (Date = {0001-01-01 00:00:00})

FullDateOfBirthLast  = "1914-9-28"

fromDateValueLast (Date = {0001-01-01 00:00:00})

What is wrong?

Hi grafic,

Please use this format:

var formats = new[] { "yyyy-MM-dd", "yyyy-M-dd", "yyyy-M-d","yyyy-MM-d" };

Best Regards

Starain

Leave a Reply