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
10
Medium Priority
?
488 Views
Last Modified: 2008-02-01
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
Comment
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
  • Learn & ask questions
10 Comments
 
LVL 18

Accepted Solution

by:
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

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

Expert Comment

by:pradeepsudharsan
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
Pradeep
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:pradeepsudharsan
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");
                  }


--pradeep
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
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");
                  }

--Pradeep
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
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");
                  }

--pradeep
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
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";

--pradeep



0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 16893733
Pradeep,

  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

by:pradeepsudharsan
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";


--pradeep
0
 

Author Comment

by:BeginningWebDesign
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question