PC Review


Reply
Thread Tools Rate Thread

DateTime range

 
 
shapper
Guest
Posts: n/a
 
      18th Oct 2008
Hello,

I have two nullable DateTime: begin and end.
I need to find the range, in month, between both variables.

If for some reason, for example begin is null, it is not possible to
calculate the range then I want to define the range as 1 month.

How can I do this?

Thanks,
Miguel
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      18th Oct 2008
shapper wrote:
> I have two nullable DateTime: begin and end.
> I need to find the range, in month, between both variables.
>
> If for some reason, for example begin is null, it is not possible to
> calculate the range then I want to define the range as 1 month.
>
> How can I do this?


I don't think there are any magic.

if(dt1 != null && dt2 != null)
{
delta = (dt2 - dt1).TotalMonths;
}
else
{
delta = 1;
}

or something similar.

Arne
 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      18th Oct 2008
On Oct 18, 6:40*pm, Arne Vajhøj <a...@vajhoej.dk> wrote:
> shapper wrote:
> > I have two nullable DateTime: begin and end.
> > I need to find the range, in month, between both variables.

>
> > If for some reason, for example begin is null, it is not possible to
> > calculate the range then I want to define the range as 1 month.

>
> > How can I do this?

>
> I don't think there are any magic.
>
> if(dt1 != null && dt2 != null)
> {
> * * *delta = (dt2 - dt1).TotalMonths;}
>
> else
> {
> * * *delta = 1;
>
> }
>
> or something similar.
>
> Arne


In VB there is a really useful function named DateDiff. In C# there is
not ...

VisualBasic assembly could always be used but I prefer not to used
it ...

So I came up with the following:

int diff = 12 * (begin.Year - end.Year) + begin.Month - end.Month;
int months = Math.Abs(diff);

Not sure if it is the best option but I think I might implement a
DateDiff method for C#
 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      18th Oct 2008
On Oct 18, 8:03*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sat, 18 Oct 2008 11:39:43 -0700, shapper <mdmo...@gmail.com> wrote:
> > In VB there is a really useful function named DateDiff. In C# there is
> > not ...

>
> In C# there is no need. *For that matter, DateDiff isn't really needed in *
> VB.NET. *It's there for legacy reasons.
>
> > VisualBasic assembly could always be used but I prefer not to used
> > it ...

>
> DateDiff doesn't address your original question. *Why would you bother to *
> try to use it?
>
> > So I came up with the following:

>
> > int diff = 12 * (begin.Year - end.Year) + begin.Month - end.Month;
> > int months = Math.Abs(diff);

>
> In what way does that address your original question?
>
> > Not sure if it is the best option but I think I might implement a
> > DateDiff method for C#

>
> Why? *What does DateDiff do for you that the operator- overload for *
> DateTime doesn't already?
>
> Pete


Sorry Pete,

But does not my code calculate the difference between two dates in
months?

int diff = 12 * (begin.Year - end.Year) + begin.Month - end.Month;
int months = Math.Abs(diff);

I didn't address the nullable DateTime yet.

DateDiff, in my opinion, just creates an "easier" way to calculate
datetime spans in various intervals ...
 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      18th Oct 2008
On Oct 18, 6:40*pm, Arne Vajhøj <a...@vajhoej.dk> wrote:
> shapper wrote:
> > I have two nullable DateTime: begin and end.
> > I need to find the range, in month, between both variables.

>
> > If for some reason, for example begin is null, it is not possible to
> > calculate the range then I want to define the range as 1 month.

>
> > How can I do this?

>
> I don't think there are any magic.
>
> if(dt1 != null && dt2 != null)
> {
> * * *delta = (dt2 - dt1).TotalMonths;}
>
> else
> {
> * * *delta = 1;
>
> }
>
> or something similar.
>
> Arne


Arne,

There isn't a TotalMonths in TimeSpan ... only TotalDays, TotalHours,
TotalSeconds, TotalMinutes and TotalMiliseconds.

That was the first thing I looked.

Thanks,
Miguel
 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      18th Oct 2008
On Oct 18, 8:17*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sat, 18 Oct 2008 12:14:03 -0700, shapper <mdmo...@gmail.com> wrote:
> > But does not my code calculate the difference between two dates in
> > months?

>
> It does appear to, yes. *But so does the code that Arne posted. *And his *
> also addresses the specific question you actually asked. *More *
> importantly, your code has a whole lot of math in it, whereas just using *
> the already-overloaded operator- as Arne suggests, you let the DateTime *
> type do all the work for you.
>
> > int diff = 12 * (begin.Year - end.Year) + begin.Month - end.Month;
> > int months = Math.Abs(diff);

>
> > I didn't address the nullable DateTime yet.

>
> No, you didn't. *But that's actually the question you appeared to have *
> asked. *A solution that doesn't take into account null values isn't a *
> solution at all.
>
> > DateDiff, in my opinion, just creates an "easier" way to calculate
> > datetime spans in various intervals ...

>
> What could be easier that just subtracting one DateTime from another?
>
> Pete


Arne code does not solve it ... It is used TotalMonths which does not
exist in C#. I could use TotalDays / 30 but that wouldn't be accurate.
That is the reason of my code ...

Or maybe as follows:

public static long DateSpan(DateInterval interval, DateTime from,
DateTime to) {

// Define span
TimeSpan span = from - to;

// Check interval
switch (interval) {
case DateInterval.Year:
return to.Year - from.Year;
case DateInterval.Month:
return (to.Month - from.Month) + (12 * (to.Year -
from.Year));
case DateInterval.Weekday:
return CorrectDateSpan(span.TotalDays) / 7;
case DateInterval.Day:
return CorrectDateSpan(span.TotalDays);
case DateInterval.Hour:
return CorrectDateSpan(span.TotalHours);
case DateInterval.Minute:
return CorrectDateSpan(span.TotalMinutes);
default:
return CorrectDateSpan(span.TotalSeconds);
}

} // DateSpan

private static long CorrectDateSpan(double number) {

if (number >= 0) {
return (long)Math.Floor(number);
}
return (long)Math.Ceiling(number);

} // CorrectDateSpan

Note: Didn't tested it yet ...

This function would be a good help specially when I am using this on a
Linq query to define some statistics.

I could include on my function an option that when one of the dates is
null or if from is after to I would return null and then I could set a
default value on my code where I am using the function.

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      18th Oct 2008
shapper wrote:
> On Oct 18, 6:40 pm, Arne Vajhøj <a...@vajhoej.dk> wrote:
>> shapper wrote:
>>> I have two nullable DateTime: begin and end.
>>> I need to find the range, in month, between both variables.
>>> If for some reason, for example begin is null, it is not possible to
>>> calculate the range then I want to define the range as 1 month.
>>> How can I do this?

>> I don't think there are any magic.
>>
>> if(dt1 != null && dt2 != null)
>> {
>> delta = (dt2 - dt1).TotalMonths;}
>>
>> else
>> {
>> delta = 1;
>>
>> }
>>
>> or something similar.


> There isn't a TotalMonths in TimeSpan ... only TotalDays, TotalHours,
> TotalSeconds, TotalMinutes and TotalMiliseconds.
>
> That was the first thing I looked.


Interesting.

It could be because TotalMonths is not very well defined.

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      18th Oct 2008
shapper wrote:
> On Oct 18, 8:17 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> wrote:
>> On Sat, 18 Oct 2008 12:14:03 -0700, shapper <mdmo...@gmail.com> wrote:
>>> But does not my code calculate the difference between two dates in
>>> months?

>> It does appear to, yes. But so does the code that Arne posted. And his
>> also addresses the specific question you actually asked. More
>> importantly, your code has a whole lot of math in it, whereas just using
>> the already-overloaded operator- as Arne suggests, you let the DateTime
>> type do all the work for you.
>>
>>> int diff = 12 * (begin.Year - end.Year) + begin.Month - end.Month;
>>> int months = Math.Abs(diff);
>>> I didn't address the nullable DateTime yet.

>> No, you didn't. But that's actually the question you appeared to have
>> asked. A solution that doesn't take into account null values isn't a
>> solution at all.
>>
>>> DateDiff, in my opinion, just creates an "easier" way to calculate
>>> datetime spans in various intervals ...

>> What could be easier that just subtracting one DateTime from another?

>
> Arne code does not solve it ... It is used TotalMonths which does not
> exist in C#.


Yep. Sorry about that.

> I could use TotalDays / 30 but that wouldn't be accurate.
> That is the reason of my code ...
>
> Or maybe as follows:
>
> public static long DateSpan(DateInterval interval, DateTime from,
> DateTime to) {
>
> // Define span
> TimeSpan span = from - to;
>
> // Check interval
> switch (interval) {
> case DateInterval.Year:
> return to.Year - from.Year;
> case DateInterval.Month:
> return (to.Month - from.Month) + (12 * (to.Year -
> from.Year));


If that is the business logic you want, then that is probably
the way to go.

It is not obvious though that there are 1 months between September
30th and Octobet 1st but 0 months between October 1st and October 31st.

Arne
 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      18th Oct 2008
On Oct 18, 8:41*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sat, 18 Oct 2008 12:32:51 -0700, shapper <mdmo...@gmail.com> wrote:
> > Arne code does not solve it ... It is used TotalMonths which does not
> > exist in C#. I could use TotalDays / 30 but that wouldn't be accurate.
> > That is the reason of my code ...

>
> Ah, right. *Sorry. *I just assumed that Arne wouldn't post code that *
> doesn't compile; that's more like me to do that, not him. *
>
> And yes, you're right...no TotalMonths for TimeSpan. *Which actually makes *
> sense, now that I think about it, since TimeSpan is not relative to any *
> specific date, and the total months for a TimeSpan would depend on the *
> specific date.
>
> My recollection is that the System.Globalization.Calendar class has some *
> date/time math functionality in it. *Whether it addresses this specific*
> question, I'm not sure, and I don't have time to check at the moment. *But *
> you might look at that in the meantime.
>
> You could certainly write the basics yourself, especially if you have only *
> a very narrow need (for example, you always know you only need months). *
> But if there's something in the framework that accomplishes it, that's *
> probably better. *For that matter, if the Calendar class doesn't work, *
> maybe referencing the VB assembly is the best solution after all (but it *
> still won't deal with the nulls ...I really did read your original *
> question as being focused on the question of dealing with null values; I *
> didn't realize you were asking about the month calculation too).
>
> Pete


Thanks Pete. I will take a look into System.Globalization.Calendar.
 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      18th Oct 2008
On Oct 18, 8:42*pm, Arne Vajhøj <a...@vajhoej.dk> wrote:
> shapper wrote:
> > On Oct 18, 8:17 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> > wrote:
> >> On Sat, 18 Oct 2008 12:14:03 -0700, shapper <mdmo...@gmail.com> wrote:
> >>> But does not my code calculate the difference between two dates in
> >>> months?
> >> It does appear to, yes. *But so does the code that Arne posted. *And his *
> >> also addresses the specific question you actually asked. *More *
> >> importantly, your code has a whole lot of math in it, whereas just using *
> >> the already-overloaded operator- as Arne suggests, you let the DateTime *
> >> type do all the work for you.

>
> >>> int diff = 12 * (begin.Year - end.Year) + begin.Month - end.Month;
> >>> int months = Math.Abs(diff);
> >>> I didn't address the nullable DateTime yet.
> >> No, you didn't. *But that's actually the question you appeared to have *
> >> asked. *A solution that doesn't take into account null values isn't a *
> >> solution at all.

>
> >>> DateDiff, in my opinion, just creates an "easier" way to calculate
> >>> datetime spans in various intervals ...
> >> What could be easier that just subtracting one DateTime from another?

>
> > Arne code does not solve it ... It is used TotalMonths which does not
> > exist in C#.

>
> Yep. Sorry about that.
>
>
>
> > * * * * * * *I could use TotalDays / 30 but that wouldn'tbe accurate.
> > That is the reason of my code ...

>
> > Or maybe as follows:

>
> > * * public static long DateSpan(DateInterval interval, DateTime from,
> > DateTime to) {

>
> > * * * // Define span
> > * * * TimeSpan span = from - to;

>
> > * * * // Check interval
> > * * * switch (interval) {
> > * * * * case DateInterval.Year:
> > * * * * * return to.Year - from.Year;
> > * * * * case DateInterval.Month:
> > * * * * * return (to.Month - from.Month) + (12 * (to.Year -
> > from.Year));

>
> If that is the business logic you want, then that is probably
> the way to go.
>
> It is not obvious though that there are 1 months between September
> 30th and Octobet 1st but 0 months between October 1st and October 31st.
>
> Arne


Yes, I didn't test it completely but I will check that. But has far as
I know that is also how it works in VB's DateDiff.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. M. Bruil Microsoft Access 2 26th Mar 2009 08:30 AM
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. M. Bruil Microsoft Access 0 25th Mar 2009 08:10 PM
DateTime Range shapper Microsoft C# .NET 1 17th Mar 2009 05:52 PM
DateTime range Bill Microsoft C# .NET 3 14th Apr 2005 04:04 AM
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. luna Microsoft ASP .NET 1 13th Feb 2004 01:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:57 AM.