Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Timespan problem

Posted on 2006-06-13
10
Medium Priority
?
491 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month8 days, 4 hours left to enroll

824 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