简体   繁体   English

如何转换日期时间格式为23:59:59

[英]how to convert datetime in format 23:59:59

I'm having two dates FromDate and toDate 我有两个日期FromDatetoDate

converting dates like following and passing as parameters to stored procedure: 转换如下的日期并将其作为参数传递给存储过程:

both variable are of DateTime FromDate, DateTime toDate variables 两个变量都是DateTime FromDate,DateTime toDate变量

initial values coming like {10/1/2013 12:00:00 AM} 初始值如{10/1/2013 12:00:00 AM}

I have converted that using below code to {10/1/2013 00:00:00} 我已使用以下代码将其转换为{10/1/2013 00:00:00}

  sparamFromDate.Value = FromDate.ToString("yyyy-MM-dd HH:mm:ss");

  sparamToDate.Value = toDate.ToString("yyyy-MM-dd HH:mm:ss");

which is giving me expected result for FromDate as : 2013-10-11 00:00:00.000 这给了我FromDate的预期结果: 2013-10-11 00:00:00.000

But I need a todate in following format how can i convert this in code? 但我需要一个以下格式的todate如何在代码中转换它?

toDate : 2013-10-11 23:59:59.000 toDate: 2013-10-11 23:59:59.000

toDate.ToString("yyyy-MM-dd 23:59:59.000");

如果toDate只是日期或者有一个时间组件,则无关紧要 - 输出将与您想要的相同。

I believe that you are asking for the toDate to be inclusive of all the minutes within that date, regardless of the actual time portion of the calendar time. 我相信您要求toDate包含该日期内的所有会议记录,无论日历时间的实际时间部分如何。

The following expression should achieve that result: 以下表达式应该实现该结果:

toDate.Date.Add(new TimeSpan(0, 23, 59, 59, 999)).ToString("yyyy-MM-dd HH:mm:ss.nnn");

Wal's answer is actually easier. 沃尔的答案实际上更容易。

You should also use the InvariantCulture to perform the ToString() operation, as you may end up with something other than ":" or "-" as date and time field separators. 您还应该使用InvariantCulture来执行ToString()操作,因为您最终可能会使用“:”或“ - ”之外的其他内容作为日期和时间字段分隔符。 Which will likely confuse your database engine. 这可能会混淆您的数据库引擎。

您可以在一天结束时获取DateTime,然后使用以下命令获取ToString():

toDate.Date.AddDays(1).AddMilliseconds(-1)
toDate.Value = toDate.ToString("yyyy-MM-dd HH:mm:ss.nnn");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM