Compare time
Hi,
Im having a lil question. Im trying to compare 2 times with eachother.
One time is in an xml file. The other one is the time at this current moment.
If the time is older than 1 month, it should remove a whole table from the xml file.
This is the code i produced so far:
static void Main(string[] args)
{
try
{
DataSet xmlFile = new DataSet();
xmlFile.ReadXml("test.xml");
foreach (DataRow dateRow in xmlFile.Tables[0].Rows)
{
string Date = dateRow["Date"].ToString();
DataRow[] tempXmlFileRows = xmlFile.Tables[0].Select("Date = " + Date);
DateTime dateNow = DateTime.Now;
int intOld = Int32.Parse(dateOld.ToString("yyyyMMdd"));
while( tempXmlFile != 0 && /*compare time */)
{
xmlFile.Tables.Remove("test");
}
xmlFile.AcceptChanges();
}
xmlFile.WriteXml("test.xml");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
This is how the xml file looks like:
<root>
<test>
<Id> 1 </Id>
<Date> 20060204 </Date>
<Time> 1200 </Time>
</test>
</root>
Can somebody help me with this problem please
Thanks in advance!

Compare time
Jon_Fisher
What exactly is the problem
Harmen Mesker
One thing you might try is removing the "yyyyMMdd" argument that you are passing into the ToString() call on old date... passing in that format string is worthless and can likely cause other issues. You should only be calling ToString() in this mannor on a DateTime object, which you are correctly doing with dateNow.
See my code above in my previous post for a basic comparison check, if you simply duplicate the code and reverse the comparison then you’ll know if the date exists in the future.
Tamirro
I tried what you said but i get an error in the console, it says:
String was not regonized as a valid DateTime.
This is the line where it goes wrong:
DateTime.ParseExact(dateRow["Date"].ToString(), "yyyyMMdd",
This is the code i have:
foreach (DataRow dateRow in xmlFile.Tables[0].Rows)
{
string Date = dateRow["Date"].ToString();
DataRow[] tempXmlFileRows = xmlFile.Tables[0].Select("Date = " + Date);
DateTime dateNow = DateTime.Now;
dateNow.ToString("yyyyMMdd");
Console.WriteLine("The date of today = " + dateNow.ToString("yyyyMMdd"));
DateTime dateXmlFile = DateTime.ParseExact(dateRow["Date"].ToString(), "yyyyMMdd",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
TimeSpan difference = dateNow.Subtract(dateXmlFile);
if (tempXmlFileRows.Length != 0)
{
Console.WriteLine("The date in xmlfile = " + Date);
Console.WriteLine("Difference = " + difference);
if (difference.TotalDays > 30)
{
Console.WriteLine("table is NOT outdated");
}
else
{
Console.WriteLine("table is outdated");
}
}
}
How can i make the first argument of parseExact variable
Thanks in advance!
Apteryx
You need only replace:
DateTime dateXmlFile = DateTime.ParseExact(dateRow["Date"].ToString(), "yyyyMMdd",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
with:
string oldDateString = dateRow["Date"].ToString();
DateTime dateXmlFile = DateTime.ParseExact(oldDateString, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
LondonSte
Paul Rony
Comparing the date of right now, with the date in the xml file in the while statement.
Edit:
Wonder why my topic has been moved to here, since the code is written in C#
Julian Horn
Hi,
Sorry if i wasn't clear bout what i want. My english is not that great.
This info helped alot. So i thank you for that.
There's one thing which is not very clear to me.
Do i have to put the 20060204 in this line:
DateTime oldDate = DateTime.ParseExact("20060204", "yyyyMMdd",
Because the date is a variable.
And about the tricky part, it should remove the whole table, not just the row.
If im not mistaking, this is how its done:
xmlFile.Tables.Remove("test");
So the last question i have is, can i make that 20060204 variable
Thank you very much!
JLJM
Hi,
Ok i removed the "yyy....". How can i work with days Because if the difference between the date in the xml file and the date of that current time it checks, it shoud remove the table.
Removing the table is not the issue, but how to make it count days is.
Can you tell me how to do that
Acartina
I need you to be more specific in your issue here Yustme because you had originally asked for information on comparison between the two dates, but not saying any specifics on how you wanted to do so or what you wanted to get out of it... because of your now saying that you are looking for a specific attribute of the difference between the two dates... something that is not easy to do with the direction we took.
Comparing two dates that are stored as integers to get the kind of information you want isn’t easy or good, which is why we shouldn’t be doing so here, instead, we want to parse the date info from your xml document into a DateTime instance that can be used with another DateTime instance to come up with an instance of the TimeSpan class which can tell you all sorts of wonderful details on the distance between two DateTimes.
In order to do this parsing we are going to use DateTime.ParseExact() and pass in the string that contains the date, the format it is in, and an Invariant culture info object. A simple demo of this would be:
DateTime oldDate = DateTime.ParseExact("20060204", "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
Taking that you would replace your line:
int intOld = Int32.Parse(dateOld.ToString("yyyyMMdd"));
with
DateTime oldDate = DateTime.ParseExact(dateOld, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
This does assume that dateOld has been populated with the correct string.
Next, we subtract the oldDate value from the dateNow value which returns a TimeSpan:
TimeSpan difference = dateNow.Subtract(oldDate);
Finally, in order to determine the days between the two associated dates, check the TotalDays property of the TimeSpan instance named difference.
This is the tricky part, afterwards you need only note the row and delete it from the table once you’ve examined them all... a task I will leave for you or another question.
DSLMaxQ
Hi,
I found the error. In the date tag i had white spaces before the date itself and after the date.
When i removed the white spaces, it worked.
This is how it looked like:
<Date> 20060204 </Date>
And i changed it to this:
<Date>20060204</Date>
How can i convert the date to an int or ignore the white spaces
Andrew Stolarz
No where in your code do you appear to be doing any kind of comparison... given that you have got the date from the XML file as an integer in intOld... you could get a string representation of the current date in dateNow, convert it to an int and compare it ala:
int intNow = Int32.Parse(dateNow.ToString("yyyyMMdd"));
if (intOld < intNow)
{
Console.WriteLine("Date occured prior to today");
}
Inkyskin_UK
Hi,
Thank you brendan.
But that method Trim(); doesn't trim the spaces if its more than 3 spaces.
How come
Bertrand JURADO
First up, do not convert the date string to an int, that would be bad bad bad for what you are trying to do... your better option would be to simply trim the whitespace from the string with the Trim() method before parsing it into a DateTime, to do that we can expand on my previous example of:
string oldDateString = dateRow["Date"].ToString();
DateTime dateXmlFile = DateTime.ParseExact(oldDateString, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
And add a single line (of course everything could be wrapped into a single one but I do it like this for clarity):
string oldDateString = dateRow["Date"].ToString();
oldDateString = oldDateString.Trim();
DateTime dateXmlFile = DateTime.ParseExact(oldDateString, "yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
Ravikumarmp
Hi,
I don't know how to do the comparison. Thats why no where in the code appeared doing any kind of comparison.
And now i have 2 ints of dates that look exactly the same.
int intOld = Int32.Parse(dateOld.ToString("yyyyMMdd"));
int intNow = Int32.Parse(dateNow.ToString("yyyyMMdd"));
But the dateNow should actually be the systems date, and not the date it gets from the xml file.
Im trying to compare the dates with eachother to find out of the date of the xml file is older than 30 days.
How can i do that