Reputation: 510
I need the user to be able to select a date, but it might happen the user doesn't know the exact day or month, but the year.
I'm thinking to create a selector with drop down lists with optional items in each one, and hold it in a DateTime
property. But I don't know how I can specify the values for the unknown parts.
I could use separate int?
fields for Day, Month and Year but I'm wondering if there is a better way.
Any ideas?
Upvotes: 0
Views: 2829
Reputation:
Try this in css
input[type=date] {
::-webkit-datetime-edit-month-field[disabled]{}
::-moz-datetime-edit-day-field[disabled]{};
::-ms-datetime-edit-day-field[disabled]{};
::-o-datetime-edit-day-field[disabled]{};
}
http://qnimate.com/guide-to-styling-html5-input-elements/
Upvotes: 0
Reputation: 32758
I would suggest you to create a custom class.
public class CustomDate
{
public int? Day { get; set; }
public int? Month { get; set; }
public int Year { get; set; }
public DateTime? Date
{
get
{
if(Year > 0 && Day.HasValue && Day.Value > 0
&& Month.HasValue && Month.Value > 0)
return new DateTime(Year, Month.Value, Day.Value);
return null;
}
}
}
Upvotes: 0
Reputation: 8419
Yes use separate fields. Having values as select month,1,2,3... 12 in mdl and select date 1,2,3...31 in ddl. No select year in ydl. Rather keep some ideal value like current year selected
By ddl,mdl,ydl I mean name of your dropdownlist for day,month,year respectively
Display a label for user guide that year selection is compulsory
Optional (example code):
When user submits, for checking month and day you should do validation as
string userDate = ""
if (ddl.SelectedIndex>0)
{ //then you can try to validate date as
try
{
string temp=ydl.SelectedItem.ToString()+"-"+ydl.SelectedItem.ToString();
temp += " "+ydl.SelectedItem.ToString()
DateTime dt=Convert.ToDateTime(temp);
userDate=dt.ToString("yyyy-MM-dd");
}
catch
{
//Display message on some label that chosen date is invalid
return;
}
}
else
{
userDate=ydl.SelectedItem.ToString();
if (mdl.SelectedIndex>0)
{
userDate += mdl.SelectedItem.ToString();
}
}
Now you can userDate (string) for the purpose you want to use
Remember here you should not use dateDatime and instead of a string like userDate because user input will become like 2012-01-01 a fixed date. In that case you can not use year only
Upvotes: 0
Reputation: 400
On the user interface, can you use a date range instead of a single date?
if they only know the year, then select a range of from 1/1 to 12/31 of that year. Either way, it would be a good idea to select a complete date, not an invalid/partial date.
Upvotes: 0
Reputation: 571
A DateTime must be a valid date so you can't just put the year in it, you need at least the day and month too. This is problematic in your case because you can't differentiate between the date 01/01/2012 and the year 2012. I don't see how you could keep the values inside a DateTime unless you also have the day and month.
The best solution here would be, like you said, to keep these values in ints and put the value 0 in the unspecified fields.
Upvotes: 4