需要有关日期时间的C#的帮助 [英] Need help with C# with datetime

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

问题描述

你好,

好吧,我在格式化datetime变量时遇到了一个大问题,以适应我的需要,无论我付出多少,我都找到了正确的解决方案.

我将向您展示代码并进行解释.
我有这种方法

Hi there,

Well i have a big problem formatting the datetime variable to suit my needs and no matter how much i dag i havnt found the right solution.

I''ll show you the code im writting and explain.
I have this method

public static List<Reservation> GetReservationsReportBySites(List<Site> sites, DateTime fromDate, DateTime toDate, DateTime fromTime, DateTime toTime, ReservationStatus status)


该方法接收了一些datetime变量,现在我试图使用它们来定义我希望应用程序显示的保留,在数据库中我也有4个datetime,其中两个是从和到目前为止的,没有实际的小时数和其他两个是没有实际时间的开始和结束时间(我无法更改它,它和工作中的旧数据库).
我该如何做,以便将用户选择的日期和时间与这四个变量进行比较,我知道我需要以某种方式打破datetime的日期和时间,但我无法做到这一点.

这是我所考虑的代码,可以对保留进行排序.


The method recieve a few datetime variables, now what im trying to do with them is to define which reservations i want the application to show, in the database i have also 4 datetime two of them are from and to date without the actual hour and the other two are the from and to time without the actual hour (i cant change it, it and old database from work).
How do i make so that i will compare the dates and time the user pick with those four variables, i know i need to somehow break the datetime to date and time but im unable to do that.

this is the code of what i was thinking that will work to sort the reservations.

var item in data.Where(p => p.StartDate.Value >=
                        fromDate.Date && p.StartDate.Value >=
                        fromTime && p.Valid == isValid &&
                                p.EndDate.Value<= toDate && p.EndDate.Value <= toTime))



如您所见,我正在尝试使用LINQ,并且在我学习的同时也学习了它,因此,我将尽我所能获得所有帮助.



as you can see im trying to use LINQ and im also learning it as i go so i''ll take all the help i will get.

thanks in advance.

推荐答案

已经尝试过这两个解决方案,但都没有解决我的问题,这是我们创建的最终对象无法获得时间跨度的原因设置为datetime.
当我用LINQ尝试时,它没有编译并给我一个错误.

我什至尝试了四个datetimepicker,两个用于fromdate和todate,两个用于fromtime和totime.

有谁知道发送仅包含时间或日期的datetime变量的另一种方法?

我尝试了所有其他可以找到的解决方案,尝试了滴答声,尝试将其转换为字符串,然后又转换回datetime,但这也没有用,因为它花费了当前的日期或时间,并且不符合我的需求.

我变得绝望了.我会采取任何其他可以给我的解决方案的方法.

再次感谢您的帮助.
Already tried those two solutions and both didnt solve my problem, the thing is the end object that we created wont recieve timespan cause it''s set to datetime.
when i tried it with the LINQ it didnt compile and gave me an error.

I even tried with four datetimepickers two for the fromdate and todate and two for fromtime and totime.

anyone know of another way to send a datetime variable with only the time or the date?

I tried every other solution i could find, i tried ticks, i tried converting to string and then convert back to datetime but that didnt work too cause it took the current date or time and it didnt suit my needs.

I''ll take any other solution you can give me, i''ve become desperate.

Thanks again for the help.


可以从日期时间中提取时间或日期.以下代码显示了一种如何从日期中提取时间以及如何从日期中删除时间的方法.

Extracting the time or date from a datetime is possible. The following code shows a way how to extract the time from a date and how to remove the time from the date.

DateTime now = DateTime.Now;
TimeSpan onlyTime = now - now.Date;
DateTime onlyDate = now - onlyTime;
Console.WriteLine(now);
Console.WriteLine(onlyTime);
Console.WriteLine(onlyDate);



希望这会有所帮助.



Hope this helps.


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

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