在 HTML 文本输入中显示 C# 日期时间
Display C# DateTime in HTML Text Input
所以我从数据库中提取了一个日期时间,我想在HTML文本输入(<input type="text">
)中显示它。
当 C# DateTime 发送到 Javascript 时,它看起来像这样:fooDate: {6/22/2011 2:30:00 PM}
. 但是当它显示在 <input>
,它最终看起来像这样: 2011-06-22T14:30:00
.
这有点烦人,因为我只关心显示日期。 为什么会发生这种转换,有没有办法让它显示6/22/2011 2:30:00 PM
位? 这可以在不使用正则表达式的情况下完成吗?
混合搭配您想要的任何组合。世界是你的牡蛎。
DateTime.Now.Day
DateTime.Now.Month
DateTime.Now.Year
你能
在将它传递给你的JS方法之前将其转换为字符串吗?
string Output = string.Format("{0:G}",DatabaseValue);
只是在日期上得到的例子...
DateTime.Now.ToShortDateString();
将返回"02/12/2014"
或者您可以指定所需的格式
DateTime.Now.ToString("yyyy-MM-dd");
将返回"2014-12-02"
尝试使用 String.Format 函数:
//Here you pull the DateTime from your db
//I show you an example with the current DateTime
DateTime myDateTime = DateTime.Now;
string date = String.Format("{0:dd-MM-yyyy}", myDateTime); //eg: 02-12-2014
//Place the string in your textbox
this.txtYourTextbox.Text = date;
如果您不知道要使用哪种格式,请参阅以下文章:
日期时间的字符串格式 [C#]
相关文章:
- 更改angularjs中的日期-时间格式
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 将日期时间从json转换为可读格式
- 使用时刻.js从日期时间中提取时间
- 两个日期时间选择器
- Javascript:如何在onChange事件期间更改日期时间值
- .Net webApi ISO日期时间和IE8
- Javascript-如何使用bootstrap日期时间选择器自动计算两个时间输入之间的差异
- 转换日期时间格式
- 在JavaScript中转换日期/时间格式
- JavaScript JSON关联对象键和值都作为日期时间戳
- 将日期时间javascript绑定到表列
- 将JS日期解析为C#日期时间
- 使用JavaScript将客户端日期/时间字符串转换为JSON日期/时间串
- 从javascript数组获取php的日期时间
- 格式化momentjs日期时间
- 这个正则表达式的日期/时间有什么问题
- Mongoose查询在两个日期时间之间提取数据
- 当转换为Python日期时间时,Node的时间戳落后3分钟
- 自定义angularjs过滤器日期时间格式额外字符