Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make datepicker display value form Json string?

String date is being passed as JSON from the controller to the view. Problem is the datepicker is not displaying the value. How to do it?

function SetCalendar(id, str) {
    var strDate = document.getElementById(id);
    var dateVal = luxon.DateTime.fromFormat(str, "MM-dd-yyyy");
    strDate.value = dateVal;
}

Tried like this, not working, how to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming that your datepicker is a standard HTML input element with type="date", you need to set the value of the input element using the format "yyyy-MM-dd" instead of "MM-dd-yyyy". Also, you should convert the DateTime object to a string using the toISODate() method before setting the value of the input element.

function SetCalendar(id, str) {
    var strDate = document.getElementById(id);
    var dateVal = luxon.DateTime.fromFormat(str, "MM-dd-yyyy");
    strDate.value = dateVal.toISODate();
}

Note that some browsers may not support the date input type or the ISO date format, in which case you may need to use a third-party datepicker plugin or a polyfill library.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading