c# - Datepicker between two ages 21 and 25 wpf -
i have made application user selects there date of birth using datepicker
. user must on 21 , under 26. if user on 21 , under 26 message box appears saying "success". when select date of birth between 21 , 25 nothing happens. not sure i'm doing wrong here think minusing 21 , 26 selected date in datepicker
my code fallows
xaml
<datepicker horizontalalignment="center" name="dpkdob" grid.column="1" verticalalignment="top" grid.row="10" />
xaml.cs
datepicker dp = new datepicker(); datetime dateminimum = dpkdob.selecteddate.value.addyears(-21); datetime datemaximum = dpkdob.selecteddate.value.addyears(-26); // 26, include below 26 years old datetime birthdate = convert.todatetime(dp.selecteddate); if (birthdate > dateminimum && birthdate < datemaximum) { messagebox.show("success"); }
you picked wrong datepicker
comparison:
datetime birthdate = convert.todatetime(dp.selecteddate);
should
datetime birthdate = convert.todatetime(dpkdob.selecteddate);
according comments:
datetime birthdate = convert.todatetime(dpkdob.selecteddate); if(birthdate > datetime.now.addyears(-26) && birthdate < datetime.now.addyears(-21)) { // stuff }
if need more in solution, think extension this:
public static class extensions { public static timespan age(this datetime dt) { return (datetime.now - dt); } public static int years(this timespan ts) { return (int)((double)ts.days / 365.2425); } }
usage:
datetime birthdate = convert.todatetime(dpkdob.selecteddate); if (birthdate.age().years() > 21 && birthdate.age().years() < 26) { // stuff }
Comments
Post a Comment