Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My table in database contains dates.
i want to remove the dates from the record if they are from before today i.e yesterday or day before yesterday and so on...

i know i can find a date in a string by splitting the word.
now how can i remove THAT date(date which i found in the string by splitting) if it is from before today??



What I have tried:

 while (READ.Read())
                {
                string date = READ.GetString(h);
                DateTime dt;
                string[] words = date.Split(' ');
                bool found = false;
                foreach (string word in words)
                {
                    if (DateTime.TryParse(word,out dt))
                    {
                        l = word;
                        found = true;
                        break;
                    }
                    
                if(found)
                {
                        result = DateTime.Compare(l, DateTime.Today);//error cannot compare string with datetime..


                    }
}
Posted
Updated 16-Jul-17 6:34am
v2
Comments
BillWoodruff 17-Jul-17 0:56am    
Why not use a dedicated DateTimePicker Control and prevent errors by not allowing them to happen: like JQuery's:

https://www.youtube.com/watch?v=DORmHaYU27c

You'll need to use Regex (regular expressions) to determine this. See the results of this[^] Google search to get started.

Also see this[^] CP answer for a quick solution.

/ravi
 
Share this answer
 
v2
Not easily, or even reliably.
The problem is that if you don;t know where the date is, or even ho0w long it it, then you presumably don't know what format it is in either - and that causes big problems.
The example you give is easy to spot: split the string on a space and check each "word" to see if it Parses as a Date value:
C#
DateTime dt;
string[] words = input.Split(' ');
bool found = false;
foreach (string word in words)
   {
   if (DateTime.tryParse(word, out dt))
      {
      found = true;
      break;
   }
But ... if the date is free form, then what stops the user entering "Hello today's date is 16th of July, 2017!"? And that's hard to find as a date, even with sophisticated checking.
If I was you, I'd redesign the input mechanism to make dates come from pickers, rather than parsing free form text.
 
Share this answer
 
If you can find the date part from the string and you know the format of date stored in database table then I think you can simply use parsedate method to convert the string to date and then compare it with current date using below code.

if(dateFoundInDB.Date ==DateTime.Now.Date)

if this condition is true use string.remove(dateFoundInDB) to remove the date from the string.
 
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