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

I have a pdf file I am generating code for extract data from pdf to excel sheet

C#
if (listBox1.Items[i].ToString().Contains("ONLY Page 1 of 5 "))
{
    dr[5] = listBox1.Items[i + 2].ToString().Trim();
}


dr[5] gives the value of date what is in pdf(ex:12/31/2016)
but i want in dr[5] subtract 7 days in dr[5] and add 15 days resulted date

For example my date is 12/07/2016
12/07/2016 - 7days + 15 days =12/15/2016


What I have tried:

C#
if (listBox1.Items[i].ToString().Contains("ONLY Page 1 of 5 "))
{
   dr[5] = listBox1.Items[i + 2].ToString().Trim();
}

I have no idea
Posted
Updated 13-Dec-16 21:26pm
v2
Comments
StM0n 14-Dec-16 3:17am    
Not quite sure what you're trying to accomplish... but why don't you just add 8 days?

What type is in dr[5]... if it is a string, you have to convert it into a DateTime then just add your days.

Convert the string to a datetime variable and use the datetime methods;

C#
DateTime date1 = DateTime.Parse("12/07/2016",
               System.Globalization.CultureInfo.InvariantCulture).AddDays(-7);
 date1 = date1.AddDays(15);


Why not just add 8 days instead of -7 + 15?

Only difficulty is making sure the string has the month and day the right way round check out MSDN.

DateTime Structure (System)[^]
 
Share this answer
 
v2
You have to convert the string representing a date to a numeric date representation (DateTime Structure (System)[^] with C#). Then use that to perform the calculation (see the DateTime.Add[xxx] functions). To print the date out use DateTime.ToString. Because you want to pass it to Excel, it would be better to use the numeric value and pass that to Excel (which might require setting the cell format depending on the used export method).

The problem is that there are many different date formats. But if you know the exact format the conversion is no problem (use DateTime.[Try]ParseExact). Otherwise use DateTime.TryParse to check if the string was recognised as date.
 
Share this answer
 
v2

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