Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Timespan problem

Posted on 2006-06-13
Medium Priority
488 Views
Hi

This is properbly an easy problem, but for me I cannot work it out.

I need to find the value between 2 dates and if greater than 364, return a message saying cannot be more than 364 days.

I just cannot seem to get it to work.

if (TimeSpan  ValidTill.Subtract(CurrentDay) > 364);
{
litError.Text = "Must be less than 1 year";
}

Regards
Caz
0
Question by:BeginningWebDesign
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 18

Accepted Solution

Ravi Singh earned 2000 total points
ID: 16893426
Hi, try:

DateTime CurrentDay = new DateTime(2006, 6, 13);
DateTime ValidUntil = new DateTime(2007, 6, 13);

TimeSpan span = ValidUntil.Subtract(CurrentDay);

if (span.Days > 364)
{
litError.Text = "Must be less than a year";
}
0

LVL 48

Expert Comment

ID: 16893457
int DiffInDays(DateTime d1, DateTime d2)
{
return d1.Subtract(d2).Days;
}
0

LVL 7

Expert Comment

ID: 16893514
Hi,
try this
DateTime a=new DateTime(2006,6,30);
DateTime b=new DateTime(2005,6,30);
TimeSpan s;
s=a-b;
int days=364;

if(s.Days  > days)
{
Response.Write("Must be less than 1 year");
}

Regards
0

LVL 7

Expert Comment

ID: 16893560
DateTime a=new DateTime(2006,6,30);
DateTime b=new DateTime(2005,6,30);
TimeSpan s=a-b;
if(s.Days  > 364)
{
Response.Write("Must be less than 1 year");
}

0

LVL 7

Expert Comment

ID: 16893597
DateTime a=new DateTime(2006,6,30);
DateTime b=new DateTime(2005,6,30);
if(((TimeSpan)(a-b)).Days  > 364)
{
Response.Write("Must be less than 1 year");
}

0

LVL 7

Expert Comment

ID: 16893640
Hi,
return d1.Subtract(d2).Days;-This will not work.

try this

DateTime a=new DateTime(2006,6,30);
DateTime b=new DateTime(2005,6,30);
if(((TimeSpan)a.Subtract(b)).Days  > 364)
{
Response.Write("Must be less than 1 year");
}

0

LVL 7

Expert Comment

ID: 16893728
DateTime a=new DateTime(2006,6,30);
DateTime b=new DateTime(2005,6,30);
litError.Text =(((TimeSpan)a.Subtract(b)).Days>364)?"Must be less than 1 year":"correct year";

0

LVL 9

Expert Comment

ID: 16893733

return d1.Subtract(d2).Days;

works absolutely fine. The only possible refinement you might want is to return the 'Abs' of the result:

return Math.Abs( d1.Subtract( d2 ).Days );

just to remove negative cases.

Wint.

ps. In your version it is uneccesary to case a.Subtract(b).days to a TimeSpan, the cast is implicit.
0

LVL 7

Expert Comment

ID: 16894250
DateTime a=new DateTime(2006,6,30);
DateTime b=new DateTime(2005,6,30);
litError.Text =(((TimeSpan)(a-b)).Days  > 364)?"Must be less than 1 year":"correct year";

0

Author Comment

ID: 16895707
Thanks Everyone

There was a lot of answers, so I went for Zephyr__  as his/hers was the first answer

Regards
Caz
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first solâ€¦
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the reâ€¦
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized â€¦
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a qâ€¦
###### Suggested Courses
Course of the Month9 days, 14 hours left to enroll