473,382 Members | 1,743 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,382 software developers and data experts.

How do I find difference in MONTH for two DataTime

Hi,

Is there is equivalent of VB's DateDiff() method in C#. I need to find
difference in months between two dates that are years apart. Docs says
that I can use TimeSpan like:

TimeSpam ts = date1 - date2;

but this gives me data in Days. I don't want to divide this number by 30
because not every month is 30 days and since the two operand values are
quite apart from each other, I am afraid dividing by 30 might give me a
wrong value.

Any suggestions???

--

You don't pay to get spam, why pay to clean it?
Visit http://www.spammarshall.com to create an account for free
<http://www.spammarshall.com>
Nov 15 '05 #1
5 19375
Ali,

You can call the DateDiff function in VB. Add a reference to
Microsoft.VisualBasic.dll. Once you do that, you can call the static
DateDiff method on the DateAndTime class in the Microsoft.VisualBasic
namespace.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ali Baba" <al*****@cricketnetwork.com> wrote in message
news:3F**************@cricketnetwork.com...
Hi,

Is there is equivalent of VB's DateDiff() method in C#. I need to find
difference in months between two dates that are years apart. Docs says
that I can use TimeSpan like:

TimeSpam ts = date1 - date2;

but this gives me data in Days. I don't want to divide this number by 30
because not every month is 30 days and since the two operand values are
quite apart from each other, I am afraid dividing by 30 might give me a
wrong value.

Any suggestions???

--

You don't pay to get spam, why pay to clean it?
Visit http://www.spammarshall.com to create an account for free
<http://www.spammarshall.com>

Nov 15 '05 #2
The VisualBasic.dll's date functions may be suited for
doing what you require relatively quickly, but its
probably just as easy to write your own. I haven't
tested this, but I have a feeling all you need is
something like this:

int monthsApart = 12*(date2.Year-date1.Year) +
date2.Month-date1.Month;

01/01/00 - 01/01/03 = 36 months apart
12/01/00 - 01/01/03 = 25 months apart
01/01/00 - 06/01/99 = -7 months apart
Jerry Negrelli
Senior Software Engineer
Data Scientific Corporation
-----Original Message-----
Hi,

Is there is equivalent of VB's DateDiff() method in C#. I need to finddifference in months between two dates that are years apart. Docs saysthat I can use TimeSpan like:

TimeSpam ts = date1 - date2;

but this gives me data in Days. I don't want to divide this number by 30because not every month is 30 days and since the two operand values arequite apart from each other, I am afraid dividing by 30 might give me awrong value.

Any suggestions???

--

You don't pay to get spam, why pay to clean it?
Visit http://www.spammarshall.com to create an account for free<http://www.spammarshall.com>

Nov 15 '05 #3
Nicholas,

This is a good idea. However, what affect is this going to have on my
deployment?

Nicholas Paldino [.NET/C# MVP] wrote:
Ali,

You can call the DateDiff function in VB. Add a reference to
Microsoft.VisualBasic.dll. Once you do that, you can call the static
DateDiff method on the DateAndTime class in the Microsoft.VisualBasic
namespace.

Hope this helps.


Nov 15 '05 #4
Ali,

None, Microsoft.VisualBasic.dll is distributed with the .NET framework.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ali Baba" <al*****@cricketnetwork.com> wrote in message
news:3F**************@cricketnetwork.com...
Nicholas,

This is a good idea. However, what affect is this going to have on my
deployment?

Nicholas Paldino [.NET/C# MVP] wrote:
Ali,

You can call the DateDiff function in VB. Add a reference to
Microsoft.VisualBasic.dll. Once you do that, you can call the static
DateDiff method on the DateAndTime class in the Microsoft.VisualBasic
namespace.

Hope this helps.

Nov 15 '05 #5
Try This:

public int MONTH = 1;
public int YEAR = 2;
public int DAY = 3;

public int dateDiff(int interval, DateTime d1, DateTime d2){
if(d1 < d2){
//Swap the operators and then return.
//I expect the larger date to appear first
return dateDiff(interval, d2, d1);
}
years = d1.Year - d2.Year;
months = d1.Month - d2.Month;

if(interval == MONTH){
return (years * 12) + months;
}

if(interval == YEAR){
return years;
}
return ts.Days;
}

--

You don't pay to get spam, why pay to clean it?
Visit http://www.spammarshall.com to create an account for free
<http://www.spammarshall.com>

Ali Baba wrote:
Hi,

Is there is equivalent of VB's DateDiff() method in C#. I need to find
difference in months between two dates that are years apart. Docs says
that I can use TimeSpan like:

TimeSpam ts = date1 - date2;

but this gives me data in Days. I don't want to divide this number by
30 because not every month is 30 days and since the two operand values
are quite apart from each other, I am afraid dividing by 30 might give
me a wrong value.

Any suggestions???

Nov 15 '05 #6

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

Similar topics

4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
14
by: expertware | last post by:
Ok! to avoid confusion I will start a new argument. Thanks!! FIREFOX 1.0.7 AND IE6 viewed through DATATIME: a summary REPORT ===============================================================...
7
by: developer | last post by:
I want to substract a number of month from a specific date. someone have a easy solution ? Thanks
6
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it is giving me the difference in TimeSpan,From...
4
by: Working_Girl | last post by:
Hi, I have a database with insurance clients and their dependents (spouses and children). We had a problem in the past with the twins and some of them have been entered with one month...
23
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
5
by: Konstantinos Pachopoulos | last post by:
Hi, does any body now any such algorith? to find difference in days from YYYYMMDD to YYYYMMDD? Or just an algorithm, that converts YYYYMMDD to seconds since the epoch? Thanks
5
by: Mike | last post by:
I use c#, V2005 How I can get difference between two dates and get value in month(s) I had found some solutions but it is not exactly what I need. private static int...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.