473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1396
DateTime dt = Convert.ToDateTime("1/1/2004");
DateTime dt1 = Convert.ToDateTime("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*******@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.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 normalizedCutoff as new DateTime( _PM_Cutoff_Date.Year, _
_PM_Cutoff_Date.Month, _
_PM_Cutoff_Date.Day)

daysLeft = normalizedCutoff.Subtract( DateTime.Today)

Console.WriteLine( "Days left: {0}", daysLeft.TotalDays)

--
mikeb
Nov 18 '05 #3

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

Similar topics

1
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...
4
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...
4
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...
1
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 ...
16
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.