简体   繁体   English

C#DateTimePicker中的自定义时间值

[英]C# Custom time value in DateTimePicker

I have my datetimepicker value showing only date and this gets the current time on my system to fill up the missing time. 我的datetimepicker值仅显示日期,这将获取系统上的当前时间以填补丢失的时间。 I need to have it as d/MM/yyyy 12:00:00 AM and d/MM/yyyy 11:59:59 PM 我需要它是d/MM/yyyy 12:00:00 AMd/MM/yyyy 11:59:59 PM

There are 2 datetimepickers: Start and End 有2个日期时间选择器:开始和结束

If you want to use those values and just compare the dates regardless of time, format your DateTimePicker to just show the date, then either: 如果要使用这些值并仅比较日期而不考虑时间,则将DateTimePicker格式化为仅显示日期,然后:

  1. Take the Date portion of the selected values and compare them to the Date portion of the input: 取得所选值的Date部分,并将其与输入的Date部分进行比较:

     if(myDate.Date >= dateStart.Date && myDate.Date <= dateEnd.Date) 
  2. Add one day to the end date and use an exclusive comparison: 在结束日期前加一天,并进行独占比较:

     if(myDate >= dateStart.Date && myDate < dateEnd.Date.AddDays(1)) 

If you don't care about the time then don't mess with it - don't try to set the time on the second date to 11:59:59 PM 如果您不关心时间,那么就不要弄乱时间-不要尝试将第二天的时间设置为11:59:59 PM

If you pick day 9/2/2015 for start and 9/3/2015 for end 如果您选择第9/2/2015天作为开始并选择9/3/2015作为结束

This will give you the start date of 9/2/2015 12:00 AM: 这将为您提供9/2/2015 12:00 AM的开始日期:

string start = dtp1.Value.Date.ToString();

This will give you the end date of 9/2/2015 11:59 PM: 这将为您提供2015年9月2日晚上11:59的结束日期:

string end = dtp2.Value.Date.AddSeconds(-1).ToString();

if you want 9/3/2015 11:59 PM you could do: 如果您想要2015年9月3日晚上11:59,可以执行以下操作:

string end = dtp2.Value.Date.AddDays(1).AddSeconds(-1).ToString();

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

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