I want to show date in dd/mm/yyyy format but it always shows mm/dd/yyyy format. Also I want to set a default date to my control on page load
My view has:
<div class="col-md-4 col-sm-4 ">
@(Html.Infragistics().DatePicker()
.ID("TDate")
.ButtonType(TextEditorButtonType.DropDown)
.Render())
<input type="hidden" value="@ViewBag.TDate" id="hdnFlag" name="hdnFlag" />
</div>
My controller has :
public ActionResult Index()
{
ViewBag.TDate = "16-MAR-2017";
return View();
}
My javascript to set the default value is:
$(document).ready(function () {
var h1 = document.getElementById('hdnFlag');
if (h1 != null) {
h1 = h1.value.replace("-", " ");
h1 = h1.split("-").join(" ");
var twd = new Date(h1);
twd.toLocaleDateString("en-GB");
var d = twd.getDate();
var m = twd.getMonth();
var y = twd.getFullYear();
$("#TDate").igDatePicker({
value: new Date(y, m, d),
dateDisplayFormat: "dd/MM/yyyy",
minValue: new Date(y - 1, m, d)
})
});
I am not able to set the date correctly also if I change the date it shows in mm/dd/yyyy format.
Can anyone please help?
Regards
Singh
Hello Singh,
Thank you for using our community.
I have created a basic MVC sample for you in order to show you how to display the value in the desired format. I’m not sure why you are initializing two times the editor. Here is a sample code that should help you:
.Value("16-MAR-2017")
.DateDisplayFormat("dd/MM/yyyy")
.DateInputFormat("dd/MM/yyyy")
Please let me know if this is working for you.
Best Regards, Marina Stoyanova, Software Developer, Infragistics, Inc.
Hi,
Thanks for your reply.
I am doing this because I want to set the default date to the control and I am getting this date(16-MAR-2017 OR 16/03/2017) from the DB therefore I have set that date in the controller to the Viewbag. I want to set the minimum date to be only 1 year back of the above DB date so that the user cannot choose a date which is more than 1 year old therefore I need to break the date so that I can pass it as:
minValue : new Date(y -1 , m, d)
I am attaching my sample solution. Can you please have a look a suggest. I have tried to set .dateInputFormat as you suggested but that didn't help much.