[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add a fraction of a year to a date object

Posted on 2007-07-31
14
Medium Priority
?
391 Views
Last Modified: 2012-06-27
Hi,


How can I add 1.5 ears to a date object in c# 1.1? Thanks
0
Comment
Question by:bowemc
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 19599103
1.5 years means ?

if it's leap year then what ?
0
 

Author Comment

by:bowemc
ID: 19599115
Today is July 31st. I have a field in my app where the user enters duration in terms of years. I want to forecast the date for them. THere fore if they enter say 1.5 I want to be able to display (as accurately as possible) the date in 1.5 years time.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 19599116
DateTime FToAdd;

                  FToAdd.AddYears(1);
                  FToAdd.AddMonths(6);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:Jase-Coder
ID: 19599119
hi take a look at the DateTime class there is a method called AddMonth which you can use

http://msdn2.microsoft.com/en-us/library/system.datetime_members(VS.71).aspx
0
 
LVL 13

Expert Comment

by:josgood
ID: 19599130
using System;
namespace QuickTest {
   class Program {

      static void Main(string[] args) {
         DateTime then = DateTime.Now.AddDays(1.5 * 365);
      }
   }
}
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 19599146
you could do

DateTime NewDate = DateTime.Now.AddMonths(1.5 * 12);
 
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 19599152
oops I never saw josgoods code..
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 19599195
if you use josgoods code you would have to check for a leap year because, as you know in a leap year there is one extra day. Therefore, you new date would be a day short if it was a leap year.
0
 

Author Comment

by:bowemc
ID: 19599273
This is what I currently have.

The app crashes when I put in the decimal point. How can I cater for this. Thanks

            private void txtDurationOverride_TextChanged(object sender, System.EventArgs e)
            {
                  DateTime dtDurationForecast = DateTime.Today;
                  int iDurationMultiplier = Convert.ToInt32(txtDurationOverride.Text);
                  int iDays = iDurationMultiplier * 12;                  
                  lblDurationForecast.Text = dtDurationForecast.AddMonths(iDays).ToString(TIMESTAMP_FORMAT);
            }
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 19599322
try

 DateTime dtDurationForecast = DateTime.Today;
                  int iDurationMultiplier = Convert.ToInt32(txtDurationOverride.Text * 10);
                  int iDays = iDurationMultiplier * 12;
                  iDays = iDays / 10;                  
                  lblDurationForecast.Text = dtDurationForecast.AddMonths(iDays).ToString(TIMESTAMP_FORMAT);

i think it crashed because you were trying to convert a floating point number
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 19599329
by multiplying by 10 you no longer have 1.5 you have 15. Now multiply that by 12 and you have 180. To remove the 0 do 180 / 10 and then you have 18 months
0
 
LVL 13

Expert Comment

by:josgood
ID: 19599353
Alternatively,
         int iDurationMultiplier = Convert.ToInt32(double.Parse(txtDurationOverride));
0
 
LVL 13

Expert Comment

by:josgood
ID: 19599373
Actually, Jase-Coder's approach is better -- it preserves one decimal place.

int iDurationMultiplier = Convert.ToInt32(double.Parse(txtDurationOverride)); will simply round and that loses the precision you entered in the control.
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 2000 total points
ID: 19599500
                       System.DateTime FTeim =
                              DateTime.Now;
                        string str = textBox1.Text;
                        double i = 0;
                        double.TryParse(str
                              ,System.Globalization.NumberStyles.AllowDecimalPoint
                              ,null,out i);
                        textEdit1.Text = FTeim.AddMonths((int)(i*12)).ToLongDateString();
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

831 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