简体   繁体   English

如何在 Ajax 响应的输入类型日期中显示日期?

[英]How to show Date in Input type date from Ajax response?

My control is,我的控制是,

<input type="date" id="DecDate" name="birthday">

I save it in db and can view it in this form ,我将它保存在 db 中,可以以这种形式查看它,

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.当我尝试更新相同的记录时,我将它填充到“DecDate”中,来自 Ajax 的响应以 json 格式。

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)/ "我提醒响应它显示我上面的日期与“ /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);
                    }

It is same time but showing in Epoch.它是同一时间,但显示在 Epoch。 You can verify it using below, https://www.epochconverter.com/您可以使用以下网址进行验证, https://www.epochconverter.com/

Use new Date(value) to convert it into Date object.使用 new Date(value) 将其转换为 Date 对象。

Follow this if needed.如果需要,请按照此操作。

How convert input type="date" in timestamp javascript-jquery 如何在时间戳 javascript-jquery 中转换 input type="date"

The date format used by HTML5 date control is like yyyy-mm-dd . HTML5 日期控件使用的日期格式类似于yyyy-mm-dd So, that might be one of the issues here.所以,这可能是这里的问题之一。 So, you can convert the DecisionDate to Date object and get the proper date format to display in control like:因此,您可以将DecisionDate转换为 Date 对象并获得正确的日期格式以在控件中显示,例如:

const d = new Date(response.DecisionDate.match(/\d+/)[0] * 1);
const formattedDate = d.getFullYear()+'-'+("0"+(d.getMonth()+1)).slice(-2)+'-'+("0"+d.getDate()).slice(-2)
$('#DecDate').val( formattedDate );

DEMO:演示:

 const decisionDate = '/Date(1583787600000)/'; const d = new Date(decisionDate.match(/\\d+/)[0] * 1); const formattedDate = d.getFullYear()+'-'+("0"+(d.getMonth()+1)).slice(-2)+'-'+("0"+d.getDate()).slice(-2) $('#DecDate').val( formattedDate );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="date" id="DecDate" name="birthday">

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 .因此,对于您的场景,您可以使用moment.js ,它是一个非常好的库,用于在JavaScript解析、验证、操作和显示日期和时间。 Please refer to the below example:请参考以下示例:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<div id="date"></div>
<script type="text/javascript">
//var DecisionDate='3/10/2020 12:00:00 AM';
var DecisionDate='March 11 2020 12:00:00 AM';
var d=moment(DecisionDate).format('MM/DD/YYYY h:mm:ss a');
$("#date").text(d);     
alert(d);
</script>

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.注意:您需要通过本地安装或使用演示中提供的 CDN 链接来引用moment.js文件。

Working DEMO: https://jsfiddle.net/01e7fwv8/工作演示: https : //jsfiddle.net/01e7fwv8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM