简体   繁体   English

计算两个日期时间 C# 之间的工资

[英]Calculate salary between two Datetimes C#

Question: Hi I am wondering if threre is a way to categorize time with a simple xor or somthing like that?问题:嗨,我想知道是否有一种方法可以用简单的异或或类似的东西对时间进行分类?

Explaination: I have a database table filled with DateTime fields PunchInTime And a PunchOut time and i am trying to get how many hours of a certain time-spans there are between those two fields, how many hours are there between 08:00 and 12:00 and how many between 12:30 and 16:30 and so on.说明:我有一个数据库表,里面装满了 DateTime 字段 PunchInTime 和 PunchOut 时间,我想知道这两个字段之间有多少小时的特定时间跨度,08:00 和 12 之间有多少小时: 00 以及 12:30 和 16:30 之间的数量等等。

I am a Junior programmer so maybe im overlooking somthing here but please Advise or tell me if there is somthing i need to explain further.我是一名初级程序员,所以也许我在这里忽略了一些东西,但请建议或告诉我是否有需要进一步解释的东西。

Edit:编辑:

Yes sorry im trying to acomplish this in C# and there are multiple timespans i am trying to get out of a long time between 2 datetimes but they are never spannig a longer period than 24 hours.是的,抱歉,我试图在 C# 中实现这一点,并且有多个时间跨度,我试图摆脱 2 个日期时间之间的很长一段时间,但它们的跨度永远不会超过 24 小时。

I have tryed myself already with simple arithmetic but ive not managed to tackle this so it works.我已经尝试过简单的算术,但我没有设法解决这个问题,所以它有效。

This is a part of a program i am trying to make for work hours so im trying to get this right the first time这是我试图为工作时间制作的程序的一部分,所以我试图第一次做到这一点

This is how you can do in c#, Create two different DateTimes and use subtract, which will give a TimeSpan.这就是您可以在 c# 中执行的操作,创建两个不同的 DateTime 并使用减法,这将给出一个 TimeSpan。 you can get hours directly from that.您可以直接从中获得小时数。

DateTime now = DateTime.Now;
DateTime firstTime = new DateTime(now.Year, now.Month, now.Day, 8,0,0);
DateTime secondTime = new DateTime(now.Year, now.Month, now.Day, 12, 0, 0);

int hours = secondTime.Subtract(firstTime).Hours;

if you need to do this in SQL, there are DATEDIFF() functions.如果您需要在 SQL 中执行此操作,则可以使用 DATEDIFF() 函数。

You can use DateTime.Subtract method.您可以使用 DateTime.Subtract 方法。 Which will give you a TimeSpan and from there you can use TimeSpan.TotalHours.这会给你一个 TimeSpan,然后你可以使用 TimeSpan.TotalHours。

Look at the DateTime.Subtract Reference Here 在此处查看 DateTime.Subtract 参考

You have to get instance of TimeSpan which has all information you want and you can do it like this:您必须获得包含您想要的所有信息的TimeSpan实例,您可以这样做:

var start = DateTime.Now;
var end = DateTime.Now.AddMinutes(15);

var diff = end - start;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM