DatePicker Format

I am using datepicker from jQuery. After the selection, I want it to display as 9Feb, 11. However, when I save it, I want it to convert it to 2/9/11. How can i do this?

When displaying dte again, it should display 9Feb again, 11. How can I do this?

My code snippet is as follows:

@Html.TextBoxFor(m => (Model.dDay.Date)) $("#dDay_Date").datepicker({ dateFormat: 'dd-M-yy' }); 
+4
source share
2 answers

You can use the "altField" parameter to fill in the hidden field in your form by simply changing the date field to hidden: <input type="hidden" name="date" id="date" value=""/> . You also set the format of the alt field using 'altFormat':

 $("#date_display").datepicker({ dateFormat: 'd M y', altField: '#date', altFormat: 'dd-M-yy' }); 
+3
source

You can add a hidden input field to the page. Using the altField and altFormat , you can send data to your page in an alternative format.

To do this, you can create your own editor template for the DateTime format. Add the DateTime.cshtml file to the Views / Shared / EditorTemplates folder.

Then you can use this content (not verified):

 @model DateTime? @using System.Globalization @{ var name = MvcHtmlString.Create(ViewData.TemplateInfo.GetFullHtmlFieldName("")); var id = MvcHtmlString.Create(ViewData.TemplateInfo.GetFullHtmlFieldId("")); var visibleID = id + "_editor"; var value = Model ?? DateTime.Today; var formattedValue = value.ToString("dd MMM yyyy"); } <input type="text" id="@(visibleID)" value="@formattedValue" /> <input type="hidden" id="@(id)" name="@(name)" value="@value.ToString("yyyy/MM/dd")" /> <script type='text/javascript'> $(function () { $('#@(visibleID)').datepicker({ dateFormat: 'd mm y', altField='@id', altFormat='d/m/y' }); }); </script> 

Then you need to use only TextBoxFor, in any DateTime field the datetimepicker will be activated.

0
source

Source: https://habr.com/ru/post/1335377/


All Articles