question

SSRS-VS avatar image
0 Votes"
SSRS-VS asked SSRS-VS edited

Set day and month on only-year-selector-boostrap-datepicker

Hi.
I have a datepicker for selecting only years (since I want to retain fix the day and month)

This is the form for my model:
104708-imagen.png

And I want fix the day and month when I select the year:

104734-imagen.png

How can I set model=>Date.value = "31/12/" + selected year in the create and edit view so I can pass the value to the controller and pass de modelSatatet.isValid???

Date field in my model:

104761-imagen.png


dotnet-aspnet-core-mvcdotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YihuiSun-MSFT avatar image
1 Vote"
YihuiSun-MSFT answered SSRS-VS edited

Hi @SSRS-VS,

You can modify your code to:

View

             <div class="col-md-10">
                 @Html.EditorFor(m => m.Date,new { htmlAttributes = new { @class = "form-control", type = "text" } })
                 @Html.ValidationMessageFor(m => m.Date, "", new { @class = "text-danger" })
             </div>

JavaScript on View

         var dp = $("#Date").datepicker({
             format: "dd/mm/yyyy",
             viewMode: "years",
             minViewMode: "years",
             autoclose: true
         });
         dp.on('changeDate', function (e) {
             $(this).datepicker("setDate", new Date(e.date.getFullYear() + 1, e.date.getMonth(), 0) )
         });

Result
105287-result.gif


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
YihuiSun


result.gif (129.3 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi. Very helpfull. I copied your code and for some reason, it don't put 31/12 it puts: 01/01. So I write:

var dp = $("#datepicker").datepicker({
format: "31/12/yyyy", <----instead of dd/mm/yyyy
viewMode: "years",
minViewMode: "years",
autoclose: true
});

Now it works. Thank you.

0 Votes 0 ·