1

I have a project on ASP.Net core MVC 2.1. And there is the following model:

public class Duty
{
    public int DutyId { get; set; }
    public DateTime Date { get; set; }
    public List<DutyOfPerson> DutyPerson { get; set; }
}

VS generated controllers and views. HTML is generated for the Date property:

<div class="form-group">
    <label class="control-label" for="Birthday">День рождения</label>
    <input class="form-control" type="date" data-val="true"  id="Birthday" 
       name="Birthday" value="">
    <span class="text-danger field-validation-valid" data-valmsg-for="Birthday" 
    data-valmsg-replace="true"></span>
  </div>

When you call the action controller displays all the duty. But now I need to display data for one month. I need to create DatePikker on the page where I can only select the month and year. Did someone do something like that? Thanks in advance.

  • Have you tried string manipulation to manually remove the days? – Feathercrown Dec 20 '18 at 15:44
  • You mean "MM / YYYY", right? – Pedro Lima Dec 20 '18 at 15:44
  • if this is being used to map to a text field in a view, which has a datepicker attached...the format string will only affect the textbox at the point the existing model value is placed into it. It doesn't affect how any JS datepicker looks, or how any validation works. And when you submit the value back again, a string in MM/YYYY will not parse as a valid DateTime object, so the user won't be able to submit the form with that data in it - it's not a complete date. – ADyson Dec 20 '18 at 15:46
  • What are you actually trying to achieve here? What is the data representing? – ADyson Dec 20 '18 at 15:46
  • this will not work without Javascript and definately don't need an attribute for this on the model. https://stackoverflow.com/questions/7372038/is-there-any-way-to-change-input-type-date-format – mvermef Dec 20 '18 at 21:19
  • @Feathercrown I do not want to show the days of the datepicker at all – Рома Матковский Dec 21 '18 at 13:30
  • @PedroLima Yes. I want to not only receive a date in this format, but give the user to select only the year and month – Рома Матковский Dec 21 '18 at 13:32

1 Answers1

0

Bootstrap Datepicker does what you want.

Install the plugin, change the input type from date to text and execute the script below on page load:

$('#birthday').datepicker({
    format: "mm/yyyy",
    minViewMode: "months"
});

The plugin documentation has other useful options.

Pedro Lima
  • 1,576
  • 12
  • 21