获取当前时间并在组合框中设置一个值 [英] Get current time and set a value in a combobox

查看:117
本文介绍了获取当前时间并在组合框中设置一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#编码,并且做了一个带有几个textebox和一个组合框的Windows窗体。我想创建一个获取当前时间的方法,并根据时间设置组合框中的值。在组合框中有三个值:

TimeZone 1

TimeZone 2

TimeZone 3



我有一个获取当前时间的代码:

string CurrentTime = DateTime.Now.ToString(hh:mm);

并且想要创建一个if语句(如果它是最好的?),它获取当前时间并设置组合框中的值。 b
$ b

如果时间是:

06:00 - 14:00组合框将获得值TimeZone 1

14 :01 - 22:00组合框将获得值TimeZone 2

22:01 - 05:59组合框将获得值TimeZone 3



如何做到这一点的任何想法?

解决方案

字面意思是你在问题中所说的。不要试图将它转换为字符串并再次解析它,它只会让你的生活更加艰难。这是一个示例代码逻辑

  var now = DateTime.Now; (now.Hours> = 6& now.Hours< = 14)
如果(now.Hours> 14&& amp;& amp;& amp; amp; ; now.Hours <= 22)
.........
else
........


I´m coding in C# and have done a Windows form with a few texteboxes and a combobox. I want to create a method that gets the current time and depending on the time sets the value in the combobox. In the combobox there are three values:

TimeZone 1
TimeZone 2
TimeZone 3

I have a code that gets the current time:
string CurrentTime = DateTime.Now.ToString("hh:mm");

And want to create a if statment (if its the best?) that gets the current time and sets the value in the combobox.

if the time are:
06:00 - 14:00 the combobox will get the value TimeZone 1
14:01 - 22:00 the combobox will get the value TimeZone 2
22:01 - 05:59 the combobox will get the value TimeZone 3

Any ideas of how to do this?

解决方案

Literally what you said there in the question. Dont attempt to convert it to string and parse it again, it will just make your life harder. Here is a sample code logic

   var now = DateTime.Now;
   if (now.Hours >=6 && now.Hours <=14)
    .....
   else if (now.Hours > 14 && now.Hours < = 22)
    .........
   else
    ........

这篇关于获取当前时间并在组合框中设置一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆