Solved

Timespan problem

Posted on 2006-06-13
10
485 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 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
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!

 
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

Suggested Solutions

Title # Comments Views Activity
C# Linq Select From List 3 141
C# Linq - Join two objects into one 3 55
How to identify a paragraph in the body of text? 2 81
C# LINQ 5 45
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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