My control is,
<input type="date" id="DecDate" name="birthday">
I save it in db and can view it in this form ,
3/10/2020 12:00:00 AM
when i try to update same record i populate it in “DecDate” from response coming from Ajax in json format.
i tried this,
$('#DecDate').val(response.DecisionDate);
its not showing me any date.
I alert response it shows me same above date as “/Date(1583787600000)/
“
I dont know why it is showing differently on alert and how should i populate it in control.
EDIT:
$.ajax({
type: "GET",
url: "@Url.Action("JqueryFillControl")",
contentType: "application/json; charset=utf-8",
data: { id: Id },
dataType: "json",
success: function (response) {
if (response != null) {
const d = new Date(response.DecisionDate);
const formattedDate = d.getFullYear() + '-' + ("0" + (d.getMonth() + 1)).slice(-2) + '-' + ("0" + d.getDate()).slice(-2)
$('#DecDate').val(formattedDate);
} else {
alert("Something went wrong");
}
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
3
Answers
It is same time but showing in Epoch.
You can verify it using below,
https://www.epochconverter.com/
Use new Date(value) to convert it into Date object.
Follow this if needed.
How convert input type="date" in timestamp javascript-jquery
The date format used by HTML5 date control is like
yyyy-mm-dd
. So, that might be one of the issues here. So, you can convert theDecisionDate
to Date object and get the proper date format to display in control like:DEMO:
So regarding your scenario, you can use moment.js which is a very good library for parsing, validating, manipulating, and displaying dates and times in
JavaScript
. Please refer to the below example:Note: You would need to refer to the
moment.js
file either by installing it locally or using the CDN link provided in the demo.Working DEMO: https://jsfiddle.net/01e7fwv8/