如何为datetime变量分配时间。 [英] How to assign a time to datetime variable.

查看:102
本文介绍了如何为datetime变量分配时间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查找一名员工的加班时间。



停机时间应该是下午5:00。(加班时间下午5:00以后应该计算)



停机时间是员工离开办公室的时间。

I want to find overtime hours of an employee.

offtime should be 5:00PM.(After 5:00PM overtime hours should calculate)

outtime is the time that the employee leave the office.

DateTime offtime = new DateTime();
DateTime outtime = dateTimePicker3.Value;
TimeSpan timeSpan = outtime - offtime;
double totalHours = timeSpan.TotalHours;
textBox4.Text = timeSpan.ToString();





我想找到加班时间。

请帮助我!



我正在寻找你的答案。



I want to find the overtime hours.
please help me!

I'm looking for your answers .

推荐答案

你已经有了正确的代码。唯一的问题是将关闭时间分配给默认时间。可能需要在相同日期减去两次,因此日期将从等式中取出。我不知道你想要再次获得什么,但是一个来自你的 DateTimePicker 。复制它,并从中提取日期,零时间部分:

http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx [ ^ ]。



然后,添加复制所需的时间。假设您知道时间精确到秒,请使用:

http://msdn.microsoft.com/en-us/library/system.datetime.addseconds.aspx [ ^ ]。



你有两个时间点,一个来自拣货员,另一个来自你已知的时间。减去它并按照你已经的方式进行。



-SA
You already have a correct code. The only problem is that offtime is assigned to the "default" time. Probably, you need to subtract two times with the same date, so the date would be taken out of equation. I don't know where you want to get another time, but one comes from your DateTimePicker. Copy it, and extract the date from it, with "zero" time part:
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx[^].

Then, add required time to the copy. Assuming you know that time with the accuracy of seconds, use this:
http://msdn.microsoft.com/en-us/library/system.datetime.addseconds.aspx[^].

You got two points of time, one from the picker, another from your known time. Subtract it and proceed the way you already did.

—SA




你可以使用下面的代码。



Hi,
You can use below code.

DateTime offtime = DateTime.Today.AddHours(1);
DateTime outtime = dateTimePicker1.Value;
TimeSpan totalhours = outtime.Subtract(offtime);
if (totalhours > TimeSpan.Zero)
    textBox4.Text = totalhours.ToString();
else
    textBox4.Text = TimeSpan.Zero.ToString();


这篇关于如何为datetime变量分配时间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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