通过连接多个与时间相关的下拉列表来计算时间

时间:2017-10-06 18:49:54

标签: c#

所以我有6个下拉列表来存储时间值。

Start time
DDL Hour - 01, 02, 03,..., 10, 11, 12
DDL Minutes - 00, 15, 30, 45
DDL TimeofDay - AM, PM

Finish Time
Same values

我正在寻找一种方法,在索引更改时,计算开始和结束之间的时间。

有没有办法用我有的下拉列表数量来做到这一点?我看过几个人使用1个下拉开始和1个完成的例子。如果所有其他方法都失败了,我可以做到这一点,但希望有一种方法可以将3连接在一起然后显示所花费的时间。

2 个答案:

答案 0 :(得分:2)

我认为最简单的方法是通过组合3个下拉列表来获取DateTime值,你可以做类似的事情

import random

def rps():
    computer_choice = random.randint(1,3)
    if computer_choice == 1:
        comuter_choice_rock()
    elif computer_choice == 2:
        comuter_choice_paper()
    else:
        comuter_choice_scissors()

def computer_choice_rock():
    user_choice = input("1 for Rock, 2 for Paper, 3 for Scissors: ")
    if user_choice == "1":
        print ("It's a Tie!")
        try_again()
    if user_choice == "2":
        print ("You Win! Paper covers Rock!")
        try_again()
    if user_choice == "3":
        print ("I Win and You Lose! Rock crushes Scissors!")
        try_again()
    else:
        print ("Please type in 1, 2, or 3")
        computer_choice_rock()

def computer_choice_paper():
    user_choice = input("1 for Rock, 2 for Paper, 3 for Scissors: ")
    if user_choice == "1":
        print ("I Win and You Lose! Paper covers Rock!")
        try_again()
    if user_choice == "2":
        print ("It's a Tie!")
        try_again()
    if user_choice == "3":
        print ("You Win! Scissors cut Paper!")
        try_again()
    else:
        print ("Please type in 1, 2, or 3")
        computer_choice_paper()

def computer_choice_paper():
    user_choice = input("1 for Rock, 2 for Paper, 3 for Scissors: ")
    if user_choice == ("1"):
        print ("You Win! Rock crushes Scissors")
        try_again()
    if user_choice == "2":
        print ("I Win! Scissors cut Paper!")
        try_again()
    if user_choice == "3":
        print ("It's a Tie!")
        try_again()
    else:
        print ("Please type in 1, 2, or 3")
        computer_choice_paper()

def try_again():
    choice = input("Would you like to play again? Y/N: ")
    if choice == "Y" or choice == "y" or choice == "Yes" or choice == "yes":
        rps()
    elif choice == "n" or choice == "N" or choice == "No" or choice == "no":
        print ("Thanks for Playing!")
        quit()
    else:
        print ("Please type Y or N")
        try_again()

rps()

然后对另一组3个下拉菜单重复此操作。找出差异:

DateTime time = DateTime.Today.AddHours(ddHours.SelectedValue).AddMinutes(ddMins.SelectedValue);
if (ddTimeOfDay.SelectedValue == "PM") time.AddHours(12);

答案 1 :(得分:1)

如果您有文本值,则可以将所有内容组合在一起解析所有内容:

var DDLStart_Hour = // start hours dropdown value
var DDLStart_Minute = // start minute dropdown value
var DDLStart_Tt = // start AM/PM dropdown value

var DDLEnd_Hour = // end hours dropdown value
var DDLEnd_Minute = // end minute dropdown value
var DDLEnd_Tt = // end AM/PM dropdown value

var now = DateTime.Now;
var start = DateTime.Parse($"{now.Month}/{now.Day}/{now.Year} {DDLStart_Hour}:{DDLStart_Minute}:00 {DDLStart_Tt}");
var end = DateTime.Parse($"{now.Month}/{now.Day}/{now.Year} {DDLEnd_Hour}:{DDLEnd_Minute}:00 {DDLEnd_Tt}");

var elapsed = end - start; // TimeSpan