【问题标题】:Set datetimepicker value using hour and minute int Variables使用小时和分钟 int 变量设置 datetimepicker 值
【发布时间】:2013-04-13 19:21:21
【问题描述】:

有问题。 我想将两个双打转换为时间格式(HH:mm)(例如,2.5 等于 02:30)。我有一个小时 int 变量和一分钟 int 变量。问题是我不知道如何处理它们,我的意思是如何将它们导入日期时间选择器。 开始时,时间存储在数组中的字符串中。

public {
    int hour = 5;
    int min =40;
    time.Value = new DateTime(hour, min);// ***this is a mistake***
}

日期选择器中的结果应该是 05:40

【问题讨论】:

标签: c# datetime int double


【解决方案1】:

不确定您的要求,但如下所示,

time.Value = DateTime.Today.AddHours(hours).AddMinutes(minutes);

【讨论】:

  • 我不想将小时和分钟添加到日期选择器中。我希望它只显示变量中的时间。非常感谢...
  • @misha312 你的回答和我的回答有什么区别?我正在获取当前日期并设置小时和分钟。最后它会给你你想要的价值
  • DateTime.Now.Date 可以替换为更简单的DateTime.Today
【解决方案2】:

想出了答案。

DetaTimePicker.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 
                   DateTime.Now.Day, hour integer, minute integer, 0);

零是分钟。

谢谢各位。

【讨论】:

    【解决方案3】:

    设置时间为 13:30

     DateTimePicker newDate = (DateTimePicker)rootcontrol;
     String format = "H:mm";
     DateTime dDate;
         try
          {
            DateTime.TryParseExact(answer, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate);
           newDate.Format = DateTimePickerFormat.Custom;
            newDate.Value = dDate;
          }
           catch (System.ArgumentOutOfRangeException)
         {
            newDate.Text = answer;
            Console.WriteLine("ERROR SET TIME:" + answer);
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      相关资源
      最近更新 更多