【问题标题】:How i convert type datetime-local to string我如何将类型 datetime-local 转换为字符串
【发布时间】:2020-05-18 13:27:39
【问题描述】:

我正在使用 Asp.net 核心 MVC 3.1,
我需要编辑我的代码。我搜索了很多,但没有找到可以解决我的问题。
首先,我有一个列类型“日期时间”
SqlServer 中的数据类似于“2020-01-01 14:22:00.000”。

将这些值上传到输入时,我收到以下错误

“输入字符串的格式不正确。”

以下是我的 index.cshtml 代码:

<div class="form-group">
  <label asp-for="shift_to" class="control-label"></label>
  <input class="shift_to" asp-for="shift_to" type="datetime-local"   />
  <span asp-validation-for="shift_to" class="text-danger"></span>
</div>

Javascript:

function calculate() {
  var  startTime = $('.shift_from')[0].value;
  var endTime    = $('.shift_to')[0].value;
  return (new Date(endTime) - new Date(startTime)) / 1000 / 60 / 60;
}

$('.shift_to').change(function () {
  $('.shift_hours').val(calculate());
});

【问题讨论】:

  • 您在哪里收到错误消息?在后端?在 SQL 中?
  • 当我加载页面时出现错误:(

标签: javascript c# asp.net-mvc asp.net-core-mvc


【解决方案1】:

FORMAT() 函数还允许您在 SQL Server 中使用format date and time

例如;

FORMAT(Created, 'dd.mm.yyyy HH:mm') AS DateCreated 
--08.24.2019 14:24


如果您需要在 JavaScript 中格式化日期:

const date = new Date(2019, 0, 23, 17, 23, 42)

toString // Wed Jan 23 2019 17:23:42 
toDateString // Wed Jan 23 2019
toLocaleString // 23/01/2019, 17:23:42
toLocaleDateString // 23/01/2019
toGMTString // Wed, 23 Jan 2019 09:23:42 GMT
toUTCString // Wed, 23 Jan 2019 09:23:42 GMT
toISOString // 2019-01-23T09:23:42.079Z

希望这会有所帮助。

【讨论】:

  • 我在哪里可以把这行放在 javascript 中?
  • 我改成:type="datetime-local" value="2020-01-01 14:22:00.000" 同样的错误
猜你喜欢
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2021-10-28
  • 2013-03-03
  • 2012-09-19
  • 1970-01-01
  • 2022-08-18
  • 2011-08-06
相关资源
最近更新 更多