在输入类型时间中显示HH:MM

时间:2021-04-03 16:27:36

I am trying to use an html input field with the following code:

我正在尝试使用带有以下代码的html输入字段:

<td><input style="width: 80px" type="time" placeholder="HH:MM" required="" data-bind="value: FirstManifest"></td>

The backend C# object containing the FirstManifest property (DateTime?) is populated correctly but when I bind it to a knock view model, the property in the model FirstManifest looks like this

正确填充包含FirstManifest属性(DateTime?)的后端C#对象,但是当我将其绑定到knock视图模型时,模型FirstManifest中的属性看起来像这样

"/Date(1464748440000)/"

How can I convert this so my input only shows the datetime format HH:MM?

我怎么能转换这个,所以我的输入只显示日期时间格式HH:MM?

2 个解决方案

#1


1  

This is how .NET serializes it DateTime i believe. You first need to convert it to JavaScript Date and then get the time element, e.g.

这就是.NET如何序列化我相信的DateTime。首先需要将其转换为JavaScript Date,然后获取时间元素,例如

var manifest 
  = new Date(parseInt("/Date(1464748440000)/".replace("/Date(", "").replace(")/",""), 10));

this.FirstManifest = ko.computed(function() {
    return manifest.toLocaleTimeString();
}, this);

you will also need the write part of the computed to convert back from the input to date for persisting to the server.

您还需要计算的写入部分,以便从输入转换回日期以便持久保存到服务器。

#2


1  

It gets converted to milliseconds.Try converting it back like this.

它被转换为毫秒。尝试将其转换回来。

var oldDate= "/Date(1464748440000)/";
var newDate = new Date(parseInt(oldDate.substr(6)));
document.write(newDate);
   

#1


1  

This is how .NET serializes it DateTime i believe. You first need to convert it to JavaScript Date and then get the time element, e.g.

这就是.NET如何序列化我相信的DateTime。首先需要将其转换为JavaScript Date,然后获取时间元素,例如

var manifest 
  = new Date(parseInt("/Date(1464748440000)/".replace("/Date(", "").replace(")/",""), 10));

this.FirstManifest = ko.computed(function() {
    return manifest.toLocaleTimeString();
}, this);

you will also need the write part of the computed to convert back from the input to date for persisting to the server.

您还需要计算的写入部分,以便从输入转换回日期以便持久保存到服务器。

#2


1  

It gets converted to milliseconds.Try converting it back like this.

它被转换为毫秒。尝试将其转换回来。

var oldDate= "/Date(1464748440000)/";
var newDate = new Date(parseInt(oldDate.substr(6)));
document.write(newDate);