将时间输入从24小时转换为秒,然后再转换为24小时格式

时间:2016-09-27 03:53:09

标签: c# time visual-studio-2015

我需要做的是在24小时制的时间窗口,即:1000-1100 将它转换为秒,我可以做。

(1000/100)* 60 = 600

(1100/100)* 60 = 660

获得25%的结果(15),我也可以这样做。 (660/4)

将25%的额外秒数添加到上一个结束时间(1100)

问题是我的输出为1100而不是1115 问题是如何将其输出为1115。

我确实使用常数来定义小时和数百,所以没有神奇的数字,这些数字只是说明数学。

    //define constants
    const int HUNDRED = 100;
    const int HOUR = 60;
    const int PERCENT = 4;
    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        // recieve start time input
        string oldStart = oldStartTime.Text;
        int oldTimeIn = int.Parse(oldStart);

        // recieve end time input
        string oldEnd = oldEndTime.Text;
        int oldTimeEnd = int.Parse(oldEnd);

        // convert start time to minutes
        int hoursStart = (oldTimeIn / HUNDRED) * HOUR;
        int minutesStart = oldTimeIn % HUNDRED;
        int totalStart = hoursStart + minutesStart;

        // convert end time to minutes
        int hoursEnd = (oldTimeEnd / HUNDRED) * HOUR;
        int minutesEnd = oldTimeEnd % HUNDRED;
        int totalEnd = hoursEnd + minutesEnd;

        // calulate 25% of travel time ( end - start )
        int extraTime = totalEnd - totalStart;
        int totalMinutes = (extraTime / PERCENT);

        // convert back to hours            
        int newEndHours = (totalEnd + totalMinutes) / HOUR * HUNDRED;

        // out put new end time
        newEndTime.Text = $"{newEndHours:d4}";

1 个答案:

答案 0 :(得分:0)

public class Operations extends Fragment
{
    RadioButton surfArea, rad, diam;
    RadioGroup radG;
    Button openSelect;

    public Operations()
    {
        // Required empty public constructor
    }

    public static Operations newInstance()
    {
        return new Operations();
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_operations_sphere, container, false);

        surfArea = (RadioButton) rootView.findViewById(R.id.RB_surfArea);
        rad = (RadioButton) rootView.findViewById(R.id.RB_Rad);
        diam = (RadioButton) rootView.findViewById(R.id.RB_Diam);
        openSelect = (Button) rootView.findViewById(R.id.btn_open_select);

        radG = (RadioGroup) rootView.findViewById(R.id.RG_group);

        openSelect.setEnabled(false);

        openSelect.setOnClickListener(new View.OnClickListener()
        { //This piece of code is for testing purposes
            @Override
            public void onClick(View v)
            {
                if (surfArea.isChecked())
                {
                    openSelect.setEnabled(true); //I detected my mistake here, so I would like to know a better way to achieve this
                    Intent sa = new Intent(getContext(), OperSphere.class);
                    startActivity(sa);
                }
            }
        });

        return rootView;
    }

    @Override
    public void onResume()
    { //Here the radio button unchecks
        radG.clearCheck();
        super.onResume();
    }
}

操作以整数形式进行,所以(660 + 15)/ 100出现为11.请记住,即使它被转换为双打,也会导致11.25,而不是11.15。

您必须添加另一部分,类似于您解构处理分钟部分的时间,所以

int newEndHours = (totalEnd + totalMinutes) / HOUR * HUNDRED;