Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello members,

need your help how to use the DateAdd function in c#.net. My problem is i have a manufactureDate and the ExpireryDate text boxes. I would like the Expiry date should calculate when 2years are added from the manufacturing date and fires the expiry date automatically in the ExpiryDate textbox

Thank you
Posted

If I understood correctly, you can first parse the text in the manufacturing date text box and then using AddYears calculate the expiration date. Something like:
C#
System.DateTime manufacturingDate;
System.DateTime expirationDate;

if (System.DateTime.TryParse("5.9.2010", out manufacturingDate)) {
   expirationDate = manufacturingDate.AddYears(2);
} else {
   // not a valid date
}

To make it more reliable you should define the format provider etc. as in DateTime.TryParse Method (String, IFormatProvider, DateTimeStyles, DateTime%)[^]
 
Share this answer
 
Comments
ssempijja 4-Sep-11 8:37am    
See this and electrify

private void Mfg_Date_TextChanged(object sender, System.EventArgs e)
{

System.DateTime MfgDate;
System.DateTime ExpiryDate;

if (System.DateTime.TryParse("5.9.2011", out Mfg_Date)) {
this.Expiry_Date = this.Mfg_Date.AddYears(2);
} else {
// not a valid date
}




}
You have the Datatype DateTime by Creating the obj for that DateTime then if that obj name is TodayDate means TodayDate.Now using this code you can get the today date
The you want to add 2 days after today means TodayDate.Now.AddDays(2) pervious 2 days means TodayDate.Now.AddDays(-1)
like this--
DateTime TodayDate = new DateTime();
TodayDate = DateTime.Now;
TodayDate.AddDays(1);
TodayDate.AddDays(-1);
 
Share this answer
 
v2
Comments
[no name] 11-Sep-13 8:17am    
If you had bothered to read the question, the OP wanted to add years not days. And you would have also seen that the question was answered in Solution 1 2 years ago.
C#
public class Example
{
   public static void Main()
   {
      TimeSpan interval = DateTime.Now - DateTime.Now.Date;
      string msg = String.Format("Elapsed Time Today: {0:d} hours.",
                                 interval);
      Console.WriteLine(msg);
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900