vb.net 文本框文本模式是 DateTimeLocal

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19163471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:20:37  来源:igfitidea点击:

Textbox textmode is DateTimeLocal

c#asp.netvb.nettextbox

提问by Dinesh Persaud

I have a textboxand i would love it to be formatted. fortunately for me, i can get this done by changing the textmode = DateTimeLocal. Exactly what I want. Additionally, I would like to load this textbox with default values rather than leaving it with dd/mm/yy __:__:__. I can change the text if it is a regular textbox (single mode) or even a datetime textbox. but i cannot change it with DateTimeLocalmode. some help please. thank you.

我有一个textbox,我希望它被格式化。对我来说幸运的是,我可以通过更改textmode = DateTimeLocal. 正是我想要的。此外,我想使用默认值加载此文本框,而不是将其保留为dd/mm/yy __:__:__. 如果文本是常规文本框 ( single mode) 甚至日期时间文本框,我可以更改文本。但我不能用DateTimeLocal模式改变它。请一些帮助。谢谢你。

回答by Bryan

You will have to format the DateTime to a valid Date and Time string that can be parsed, here is one that works:

您必须将 DateTime 格式化为可以解析的有效日期和时间字符串,这是一个有效的:

txtDateTimeLocal.Text = DateTime.Now.ToLocalTime().ToString("yyyy-MM-ddTHH:mm");

For more details on what other attributes you can set, see: http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#local-date-and-time-state-(type=datetime-local)

有关您可以设置的其他属性的更多详细信息,请参阅:http: //www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#local-date -and-time-state-(type=datetime-local)

回答by Ahmd Hussein

Try setting the whole datetime format including seconds and milliseconds, worked for me.

尝试设置包括秒和毫秒在内的整个日期时间格式,这对我有用。

txtStartTime.Text = DateTime.Today.ToString("yyyy-MM-ddTHH:mm:ss.ss");

回答by Sharad Chougale

If you want to set a TextBox's DateTime dynamically from a database then you can use this code:

如果要从数据库动态设置 TextBox 的 DateTime,则可以使用以下代码:

DataTable dtTemp = (DataTable)ViewState["DataSet"]; //  this are temporary table

TextBox txtFromDate = (TextBox)gridEditBloackHomework.Rows[e.NewEditIndex].FindControl("txtFromDate");
TextBox txtToDate = (TextBox)gridEditBloackHomework.Rows[e.NewEditIndex].FindControl("txtToDate");
string c = dtTemp.Rows[e.NewEditIndex]["FromDate"].ToString();
DateTime FromDate = Convert.ToDateTime(dtTemp.Rows[e.NewEditIndex]["FromDate"]);
DateTime ToDate = Convert.ToDateTime(dtTemp.Rows[e.NewEditIndex]["ToDate"]);
DateTime dtFromDate = FromDate.AddHours(-2);
DateTime dtToDate = ToDate.AddHours(-2);
txtFromDate.Text = dtFromDate.ToLocalTime().ToString("yyyy-MM-ddTHH:mm").ToString();
txtToDate.Text = dtToDate.ToLocalTime().ToString("yyyy-MM-ddTHH:mm");