www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get months / years between two dates.

reply bauss <jj_1337 live.dk> writes:
How do you exactly do that?

Like if I have two dates as std.datetime.DateTime

How will I get the months or years between the two dates?

I was surprised to learn that Duration does not support them and 
only has weeks, days etc. but not months or years.

I can't seem to find any standard way of doing it.

Will I have to calculate that myself or? I feel like that's a big 
thing missing if it's not there already.

My use-case is simple. I need to calculate years/months since a 
specific date has happened.
May 06 2020
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 6 May 2020 at 19:51:01 UTC, bauss wrote:
 How will I get the months or years between the two dates?
What's the length of a month or a year? That's the tricky part - they have variable lengths. So a difference of one month is not super precise. You could probably just do days / 365 or days / 30 to get a reasonable approximation but that's why the library doesn't give you an easy pre-made answer.
May 06 2020
parent bauss <jj_1337 live.dk> writes:
On Wednesday, 6 May 2020 at 19:58:59 UTC, Adam D. Ruppe wrote:
 On Wednesday, 6 May 2020 at 19:51:01 UTC, bauss wrote:
 How will I get the months or years between the two dates?
What's the length of a month or a year? That's the tricky part - they have variable lengths. So a difference of one month is not super precise. You could probably just do days / 365 or days / 30 to get a reasonable approximation but that's why the library doesn't give you an easy pre-made answer.
I actually found something. I can't rely on "approximation" unfortunately. DateTime apparently has a "diffMonths" function that can be used.
May 06 2020
prev sibling parent reply WebFreak001 <d.forum webfreak.org> writes:
On Wednesday, 6 May 2020 at 19:51:01 UTC, bauss wrote:
 How do you exactly do that?

 Like if I have two dates as std.datetime.DateTime

 How will I get the months or years between the two dates?

 I was surprised to learn that Duration does not support them 
 and only has weeks, days etc. but not months or years.

 I can't seem to find any standard way of doing it.

 Will I have to calculate that myself or? I feel like that's a 
 big thing missing if it's not there already.

 My use-case is simple. I need to calculate years/months since a 
 specific date has happened.
A month doesn't have a specific amount days/weeks so "what is a month" cannot be answered from just the duration alone. You can use SysTime.diffMonths or DateTime.diffMonths to calculate the number of months between two dates. Years are always 12 months so you don't have to worry there and can just divide by 12 If you also want days or weeks, you will need to calculate that yourself afterwards
May 07 2020
parent bauss <jj_1337 live.dk> writes:
On Thursday, 7 May 2020 at 11:40:36 UTC, WebFreak001 wrote:
 On Wednesday, 6 May 2020 at 19:51:01 UTC, bauss wrote:
 How do you exactly do that?

 Like if I have two dates as std.datetime.DateTime

 How will I get the months or years between the two dates?

 I was surprised to learn that Duration does not support them 
 and only has weeks, days etc. but not months or years.

 I can't seem to find any standard way of doing it.

 Will I have to calculate that myself or? I feel like that's a 
 big thing missing if it's not there already.

 My use-case is simple. I need to calculate years/months since 
 a specific date has happened.
A month doesn't have a specific amount days/weeks so "what is a month" cannot be answered from just the duration alone. You can use SysTime.diffMonths or DateTime.diffMonths to calculate the number of months between two dates. Years are always 12 months so you don't have to worry there and can just divide by 12 If you also want days or weeks, you will need to calculate that yourself afterwards
Yah if you saw my second reply that's what I said I found. It did exactly what I needed it for. What I was hoping for originally was that the duration had some "date" knowledge but that was just me being optimistic but as diffMonths solves the problem then there is no issue.
May 09 2020