c# - 如果未写入时间,如何在日期时间控件中添加当前时间

标签 c# asp.net gridview webforms sqldatasource

我有一个 GridViewSqlDataSource

我有一个列类型:DateTime。如果我在 TextBox 中仅输入日期,则时间将为 00:00:00。如果没有指定,有没有办法自动添加当前时间?

谢谢

最佳答案

当您在 TextBox 中仅键入日期时,您仍然可以提交并将当前时间添加到日期中。

DateTime inputDate = // However you are gathering the date ;
DateTime dateWithCurrentTime = inputDate.Add(DateTime.Now.TimeOfDay);

DateTime 有一个 Add 函数,允许您添加 TimeSpan 来获取新的 DateTime 对象。

如果将当前时间 (DateTime.Now.TimeOfDay) 添加到您收集的输入中,您将获得一个新的 DateTime 对象,其当前时间为日期和输入的日期。

关于c# - 如果未写入时间,如何在日期时间控件中添加当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11108958/

相关文章:

c# - 通过 BHO 从 Outlook 拖放到 Internet Explorer 在 x32/86 机器上不起作用

c# - 返回错误答案的日期差异

c# - 从物理文件实例化 IFormFile

android - gridview的每个网格中只出现一张图像

wpf - 使用 ListView 和 GridView 删除额外的 "Space"

android - 如何减少android中gridview中的垂直空间

c# - 有时想要序列化 ​​Json 包括 [JsonIgnore]

c# - Google 通讯录数据 API 401 错误

c# - 通过属性(不是 Id)在 aspx 中查找元素

asp.net - 通过asp.net Web服务将大数据发送到ios应用程序