Solved

Timespan problem

Posted on 2006-06-13
10
477 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
10 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
Comment Utility
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
Comment Utility
       int DiffInDays(DateTime d1, DateTime d2)
        {
            return d1.Subtract(d2).Days;
        }
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
Comment Utility
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
 
LVL 7

Expert Comment

by:pradeepsudharsan
Comment Utility
     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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Expert Comment

by:pradeepsudharsan
Comment Utility
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
Comment Utility
     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
Comment Utility
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
Comment Utility
     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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now