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

Finding difference of 2 dates in months using GregorianCalendar

I want to find difference between 2 dates in number of months using GregorianCalendar utility. I want it to work fine considering the year having 28, 29, 30 or 31 days in month. It should give me exact difference in all possible cases. Please help soon. Thanks in advance.
Mar 13 '08 #1
2 1973
Wagz
11
I want to find difference between 2 dates in number of months using GregorianCalendar utility. I want it to work fine considering the year having 28, 29, 30 or 31 days in month. It should give me exact difference in all possible cases. Please help soon. Thanks in advance.
I'd enter the dates as a Date type and simply subtract the two. This will give you the difference in days, hours, minutes, seconds, milliseconds. It doesn't however give you the difference in months. To do that I might do something like

date2.Year*12 + date2.Month - date1.Year*12 - date1.Month

Wagz
Mar 13 '08 #2
Plater
7,872 Expert 4TB
If you have two DateTime objects, when you subtract them you can typecast it as a TimeSpan. TimeSpan objects have a property where you can express it in the form of whole and fractional months.
Mar 13 '08 #3

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

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...
1
by: Graeme Longman | last post by:
Hi everyone, I was wondering if anyone has written some Python code which uses a start date and end date and a given interval (day, month or year) and outputs all the time periods for that range...
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
7
by: bryanilton | last post by:
I was hoping someone could help me with this.. I'm not very familiar with javascript but I'm pretty sure it can help me with what I need to accomplish. I've tried piecing several pieces of script...
4
by: Russell | last post by:
I have an assignment that I have to complete. I have to write a windows app in C#. Here is the spec: 1/ Date Comparison Build a program that will find the number of days between two dates. You...
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...
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...
3
by: bootzwiz | last post by:
How can we find months difference between two dates. For Exp : Start Date : 05/03/2007 End Date : 06/10/2007 How to calucate months difference for any dates?.
4
by: sniipe | last post by:
Hi! I am new in Python, so I count for your help. I need to get difference in months between two dates. How to do it in python? I am substracting two dates, for example date1 - date2 and I got...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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:
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.