ASP.net-C#设置输入类型:datetime-local为当前日期

Kat*_*ine 2 c# asp.net datetime bootstrapping datetime-format

这是我在aspx文件中声明文本字段的方式:

<input type="datetime-local" class="form-control" id="inputDate" runat="server">
Run Code Online (Sandbox Code Playgroud)

并在我的aspx.cs文件的页面加载中:

inputDate.Value = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
Run Code Online (Sandbox Code Playgroud)

我的目标是,当页面加载时,inputDate字段将使用“ MM / dd / yyyy hh:mm tt”格式包含当前日期

但这不起作用。没事

Kri*_*ana 7

输入datetime-local的日期时间要求大写字母T。在这里阅读:https : //www.w3.org/TR/html-markup/input.datetime-local.html

范例:2016-03-23T05:47:48

因此您可以执行以下操作:

var localDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(' ','T'); 
inputDate.Value = localDateTime;
Run Code Online (Sandbox Code Playgroud)

  • 您只需将 T 添加到格式说明符即可。`yyyy-MM-ddTHH:mm` (2认同)