![](/img/trans.png)
[英]How to set DateTimePicker to certain time and keep the customized date?
[英]How to set DateTimePicker to certain time and keep the current date?
單擊按鈕時,我想將DateTimePicker
設置為顯示日期和時間,如下所示:
date
=當前日期。
time
= 10:30:00
以下代碼更改日期和時間但未保留當前日期:
private void button1_Click(object sender, EventArgs e)
{
dateTimePicker1.Value = new DateTime(2010, 1, 1, 10, 30, 0);
}
我想要的是:如何自定義時間到10:30:00
並保持當前日期(今天的日期)?
您可以將DateTime.Today
與DateTime.AddHours
一起使用。
dateTimePicker1.Value = DateTime.Today.AddHours(10.5);
此屬性設置為今天的日期,時間組件為午夜。 添加10.5
小時將返回當前日期作為日期與10:30
作為時間。
dateTimePicker1.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 10, 30, 0);
如果它僅用於顯示目的,請使用DateTimePicker的屬性Format = 'Custom'
和CustomFormat = 'yyyy-MM-dd HH:00:00'
。 這將顯示當前日期和當前小時(控件的值保持固定日期時間)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.