Solved

Timespan problem

Posted on 2006-06-13
10
484 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
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 38
2 questions 10 30
C# LINQ ForEach() question 6 55
Coding for the first time 9 35
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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