473,385 Members | 2,069 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,385 software developers and data experts.

Date Diff in C# and Asp.net

77
Dear All

I have two dates ile Start Date and End Date.

I want to get the difference of the 2 dates in months.....How do we do it...

For eg.

Start Date = 01/11/2009
End Date: 01/01/2010;

The answer i want is 2 months

Help requested.

Thanks in advance

Sajit
Nov 19 '09 #1
4 2529
PRR
750 Expert 512MB
Convert both the dates to Datetime using
Expand|Select|Wrap|Line Numbers
  1. Convert.ToDateTime();
  2.  
use TimeSpan to calculate the difference. Then you can get the number of days by Days property of TimeSpan object.
Nov 19 '09 #2
Curtis Rutland
3,256 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. DateTime dt1 = new DateTime(2009,10,19), 
  2.     dt2 = new DateTime(2009,11,19);
  3. TimeSpan ts = dt2.Subtract(dt1);
  4. int months = ts.Days / 30;
It isn't perfect, since not every month is 30 days, but it's close enough.
Nov 19 '09 #3
Plater
7,872 Expert 4TB
Adapt InsertAlias's code to us ts.TotalDays
TotalDays is the full timespan expressed as whole and fractional days
Nov 19 '09 #4
Curtis Rutland
3,256 Expert 2GB
Good point, I used the wrong one.
Nov 19 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ralph Freshour | last post by:
What's a good way to calculate the number of days between two dates in the following format: 2003-07-15 2003-08-02 I've looked at the PHP date functions but I'm still a bit lost...
6
by: Pete | last post by:
Hi Guys I have a form which must calc the difference between 2 date fields and return the result in a third field. I have the following code but it does not seem to work. Can anyone tell this...
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...
28
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
5
by: A | last post by:
Hi Group, Coming from a VB background I am looking for the quickest path to do a date diff in C#. I see that C# has no date diff function so this must be more obvious than it looks. Thanks
3
by: Raghu Raman | last post by:
Hi I want to find the difference between 2 dates in terms of year.It is confusng in msdn.could u help me on that. for ex, int year=datediff("y",date1,date2)//of course it is in VB. ...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
2
by: yxq | last post by:
Hello, I found there are some date formats in the email header, for example: Fri, 23 Sep 2005 08:51:56 +0800 Sat, 17 Sep 2005 09:08:07 Wed Oct 19 13:40:23 2005 19 Oct 2005 13:40:23 +0000...
7
by: Gucci | last post by:
<?php /** * check ine year is a leap year, and return the month day array * * @param int $year **the year must bigger than zero** * @return array */ function is_leap_year($year){...
6
by: Jeremy Sanders | last post by:
Hi - I need to add support to a program for dates and times. The built-in Python library seems to be okay for many purposes, but what I would like would be Unix epoch style times (seconds relative...
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:
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
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: 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:
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...
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
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,...
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.