C#datetimerpicker时间差异 [英] C# datetimerpicker time diffrence

查看:110
本文介绍了C#datetimerpicker时间差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在几小时内从日期时间选择器获得时间差异



我将此作为自定义格式:MM-dd-yyyy | hh:mm:ss



但我有点压垮我的想法



例如我想要

第一次约会:1-1-2016 | 23:00:00

第二次约会:3-1-2016 | 1:00:00



我希望它能在几个小时内查看差异



这里是26小时(24 + 2小时),但我怎么做代码呢?



这是一个DateTimePicker



我尝试了什么:



我挣扎的意思是



我可以做到23:00:00 - 1:00:00但是那时候还有挣扎的日子



有2天它和第一天之间显然不是24小时



基本上我需要的是



首次约会和放大器;时间comape与第二次约会&时间



从那时起到第二次约会已经过了几个小时



我尝试用DateTime做点什么。比较但是没有成功的

im trying to get the time diffrence from a datetime picker in hours

I got this as custom format: MM-dd-yyyy | hh:mm:ss

But im kinda crushing my mind

For example i want
First date: 1-1-2016 | 23:00:00
Second date: 3-1-2016 | 1:00:00

I want it to check the diffrence in hours

Here it is 26 hours (24 + the 2 hours) but how would i do this in code?

This is a DateTimePicker

What I have tried:

The ting im struggling with i mean

I can do the 23:00:00 hours - the 1:00:00 but then there is the struggle with the days

There are 2 days between it and the first day is obviously not 24 hours

Basicly i jused need

First date & time comape with Second date & time

How many hours have passed from then to the second date

I tried somthing with the DateTime.Compare but that didnt work out great

推荐答案

时间没有格式。时间不是字符串,而是数据结构, System.DateTime 。你唯一的问题是考虑格式。所有你需要它忘记它(直到你需要输出一些表示时间点或日期的字符串)。所有控件如日期/时间选择器都使用此类型。



时差是作为不同结构的实例生成的,系统。时间跨度;重要的是不要将时间跨度,持续时间与时间,时间尺度上的一点混淆(尽管从技术上讲,它也是历史中某一点的时间跨度,但是这些类型是从这个实现细节中抽象出来的)。时间差是使用为 System.DateTime 定义的运算符' - '(减号,减法)计算的:

Time does not have format. Time is not a string, it is a data structure, System.DateTime. Your only problem is thinking about "formats". All you need it to forget it (until you need to output some string expressing the point in time, or just date). All the controls like date/time pickers use this type.

The "time difference" is produced as an instance of different structure, System.TimeSpan; it's important not to confuse the time span, duration, with the time, a point on the time scale (even though technically it's also the time span from certain point in history, but the types are abstracted from this implementation detail). The time difference is calculated using the operator '−' (minus, subtraction) defined for System.DateTime:
System.DateTime before, after;
// initialize both variables, via DateTimePicker 
// or any other way
// ...
System.TimeSpan timeSpent = after - before;



您也可以交换操作数;它的时差不为零,其中一个减法结果为负数,另一个为正数,如果之后的表示的时间晚于之前。其他算术运算符也以自然的方式定义。



这就是全部。至于字符串表示,请查看 System.DateTime 文档并阅读有关方法 ToString Parse ParseExact TryParse TryParseExact ,所有这些。



-SA


You can also swap the operands; it the time difference is not zero, one of the results of subtraction will be negative and another positive, if after represents later moment of time than before. Other arithmetic operators are also defined, in a natural way.

That's all. As to the string representation, look at the System.DateTime documentation and read about the methods ToString, Parse, ParseExact, TryParse and TryParseExact, all of them.

—SA


这篇关于C#datetimerpicker时间差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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