473,612 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Date Calculation

IN VB.Net, you can use the DateDiff method to calculate the difference between 2 days. How do you do this in C#? I want to find the number of days between 2 dates. I there a 0 days, then I want to know the number of hours and minutes between 2 dates

I see there is a method for the DateTime object which allows me to subtract, but it requires a TimeSpan object, andI am not quite shure how to create this correctly. I presume it goes something like this..
daysLeft = _PM_Cutoff_Date .Subtract(new TimeSpan( ?, ?, ?, ?) where ? = int of Days, hours, minutes and seconds

Can someone clear up my confusion on how to do this calculation

Thanks a bunch!!
Nov 18 '05 #1
2 1406
DateTime dt = Convert.ToDateT ime("1/1/2004");
DateTime dt1 = Convert.ToDateT ime("3/1/2004");

TimeSpan ts = dt1.Subtract(dt );

Note, you can use compare to see which date is larger, if either could be
larger.

You also have this static method:

TimeSpan ts = System.DateTime .op_Subtraction (dt, dt1);
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** ***
Think Outside the Box!
*************** *************** *************** ***
"Issac Gomez" <an*******@disc ussions.microso ft.com> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
IN VB.Net, you can use the DateDiff method to calculate the difference between 2 days. How do you do this in C#? I want to find the number of
days between 2 dates. I there a 0 days, then I want to know the number of
hours and minutes between 2 dates.
I see there is a method for the DateTime object which allows me to subtract, but it requires a TimeSpan object, andI am not quite shure how to
create this correctly. I presume it goes something like this... daysLeft = _PM_Cutoff_Date .Subtract(new TimeSpan( ?, ?, ?, ?) where ? = int of Days, hours, minutes and seconds.
Can someone clear up my confusion on how to do this calculation?

Thanks a bunch!!


Nov 18 '05 #2
Issac Gomez wrote:
IN VB.Net, you can use the DateDiff method to calculate the difference between 2 days. How do you do this in C#? I want to find the number of days between 2 dates. I there a 0 days, then I want to know the number of hours and minutes between 2 dates.

I see there is a method for the DateTime object which allows me to subtract, but it requires a TimeSpan object, andI am not quite shure how to create this correctly. I presume it goes something like this...
daysLeft = _PM_Cutoff_Date .Subtract(new TimeSpan( ?, ?, ?, ?) where ? = int of Days, hours, minutes and seconds.

Can someone clear up my confusion on how to do this calculation?


You can also subtract 2 DateTimes to get a TimeSpan representing the
difference:

Dim daysLeft as TimeSpan

' remove any clock-time components from the cutoff date
' so subtracting DateTime.Today doesn't result in
' a 'fractional' TimeSpan

Dim normalizedCutof f as new DateTime( _PM_Cutoff_Date .Year, _
_PM_Cutoff_Date .Month, _
_PM_Cutoff_Date .Day)

daysLeft = normalizedCutof f.Subtract( DateTime.Today)

Console.WriteLi ne( "Days left: {0}", daysLeft.TotalD ays)

--
mikeb
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
17318
by: Raghu | last post by:
Hello... I am running into a problem while running a query..can some1 help.. this is the query : ************** SELECT * from Table S where S.dtDate1 BETWEEN dateadd(year,1,dateadd(month,-1,getdate())) AND dateadd(day,-1,(dateadd(month,1,dateadd(year,1,dateadd(month,-1,getdate()))))) *************** (first part of the date calculation comes out to be '2005-05-01' and
4
5364
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
4
15717
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e....
1
2646
by: c8tz | last post by:
This is a query I have created that picks up the top 3 dates for this data (for example) : Tree Pos2 Date 1 15 23/08/2005 1 20 12/02/2006 1 32 15/10/2006 2 17 23/08/2005 2 18 12/02/2006
16
4446
by: W. eWatson | last post by:
Are there some date and time comparison functions that would compare, say, Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd) Is 02/11/07 the same as 02/11/07? Is 14:05:18 after 22:02:51? (24 hour day is fine) How about the date after 02/28/04 is 02/29/04, or the date after 09/30/08 is 10/01/08?
0
8617
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8254
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6082
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5537
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4111
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2555
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1699
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1416
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.