8 Replies - 647 Views - Last Post: 05 October 2012 - 11:46 AM Rate Topic: -----

#1 regex  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 33
  • Joined: 25-September 12

Date Format

Posted 04 October 2012 - 10:00 AM

9/18/2012 2:40:44 PM

I have this date. I want to strip the days, compare to current date, and ensure no more than 14 days are between the two dates.

What's the regex to strip the day value from this format. I will than convert it to a variable and do the same with the current date.

It's from a report.
Is This A Good Question/Topic? 0
  • +

Replies To: Date Format

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Date Format

Posted 04 October 2012 - 10:07 AM

TimeSpan allows you to calculate 2 dates. From there you can get the Days between the two.

Edit: Sorry, I missed the part about regex.

But I still stand behind the TimeSpan structure to get the difference between 2 dates as a solution ;)

This post has been edited by CharlieMay: 04 October 2012 - 10:17 AM

Was This Post Helpful? 2
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Date Format

Posted 04 October 2012 - 11:09 AM

Regex isn't the answer to every problem. Regex with dates is actually a very bad idea.

Also, I'm curious about this line:

Quote

What's the regex to strip the day value from this format. I will than convert it to a variable and do the same with the current date.


What do you mean by stripping days? Do you mean you want to extract them? Or just remove them from the date?

If all you're looking for is the number of days between two dates, you're better off using the built in classes:

var date = DateTime.Parse("9/18/2012 2:40:44 PM");
var now = DateTime.Now;
//find difference
var difference = now - date;
Console.WriteLine("There are {0:#.##} days between {1} and {2}.", difference.TotalDays, date, now);



If by "strip" you mean remove that information from the date, that's pretty easy too:

var date = DateTime.Parse("9/18/2012 2:40:44 PM");
var stripped = new DateTime(date.Year, date.Month, 1, date.Hour, date.Minute, date.Second, date.Millisecond);
Console.WriteLine("{0,-10}{1}{2}{3,-10}{4}", "Date:", date, Environment.NewLine, "Stripped:", stripped);


Was This Post Helpful? 2
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Date Format

Posted 04 October 2012 - 11:23 AM

I'm betting the OP wanted to get the days and subtract the two numbers and see if the result is over 14.

The problem with this however is what happens if the first date is September 30th 2012 and todays date is October 4th 2012. That would give you 30 - 4 = 26 when actually only 4 days have passed.

This is why you should use the entire date as Curtis Rutland and I pointed out.

This post has been edited by CharlieMay: 04 October 2012 - 11:23 AM

Was This Post Helpful? 1
  • +
  • -

#5 regex  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 33
  • Joined: 25-September 12

Re: Date Format

Posted 04 October 2012 - 04:15 PM

Thank you all for commenting.

Yes, that's the very problem I sort of ran into with the next month issue Charlie. I like this technique. I have to make sure that any report older than 14 days isn't visible on the system. So if I use this:

var difference = now - date;

I should be fine, right?

Thank you Curtis.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Date Format

Posted 04 October 2012 - 04:21 PM

Assuming date was some time in the past, yes. difference will be a TimeSpan object. difference.TotalDays will give you the total amount of days between the two.
Was This Post Helpful? 1
  • +
  • -

#7 regex  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 33
  • Joined: 25-September 12

Re: Date Format

Posted 05 October 2012 - 08:53 AM

So I got the majority of this working. I am running into a final problem unrelated to the date format. I want to thank you each for helping. Here is my final code. I'm iterating through a table and grabbing the date. However regardless of how I'm fixing the index positions I seem to get an index out of range error.

int indexx = -11;
        	 int indexxTmp = 0; 
        	 int offSet = 11;
        	 int offSet2 = 8; 
        	 int maxLoop = 0; 
        	 int multi = indexx + offSet2 - 1; 
        	 string found = string.Empty;
        	 string[] textStrArr = {"Created", "Incomplete", "Complete", "Cancelled Without QA"};
        	 string[] textStrArr2 = {};
        	 string[] textStrArr3 = {};
        	 string tmpStr = string.Empty;
        	 string differenceStr = string.Empty; 
        	 string differenceTotStr; 
        	 int differenceTot = 0; 
        	 var grid = repo.portal.buttons.Column_Headings.rgMasterTable;
        	 var editQA = repo.portal.Order_Details.EditQA;
        	 var viewQA = repo.portal.Order_Details.View;
        	 var date = System.DateTime.Parse("9/18/2012 2:45:44 PM");
        	 var now = System.DateTime.Now;
        	 var difference = now - date; 
        	 //System.DateTime = now - date; 
        	  
	         IList<TrTag> myList = grid.FindDescendants<TrTag>(); 
	         IList<TdTag> myList2 = grid.FindDescendants<TdTag>();

	         differenceStr = difference.ToString();
	         differenceTot = Convert.ToInt32(difference.TotalDays);
	         differenceTotStr = differenceTot.ToString(); 
	         
//	         foreach(Ranorex.TdTag tag in myList2 )
         	 {  

//	         		myList2[1].MoveTo(); 
//	         		myList2[1].PerformClick();
	         		
	         	maxLoop = (int)(myList2.Count / (offSet));
         		    
	         		for (int j = 0; j < maxLoop - 1; j++)
	         		{

//	         			indexxTmp = indexx / maxLoop;
				         
				        indexx += offSet;
				         			
				        myList2[indexx].MoveTo();
				         			
				        tmpStr = myList2[indexx].InnerText;
	         
	         			if(tmpStr == textStrArr[0].ToString())
	         			{
	         				myList2[8].MoveTo();
	         				myList2[8].GetInnerHtml(); 
	         				
	         				now = System.DateTime.Parse(myList2[8].GetInnerHtml());
	         				Console.WriteLine("There are {0:#.##} days between {1} and {2}.", difference.TotalDays, date, now);
	         				Report.Info(differenceStr); 
	         				Report.Info(differenceTotStr); 
	         				
			         		if(difference.TotalDays > 14)
			         		{
			         			Report.Failure("fail"); 
			         		}
	         			}
	         			
	         			
	         			Report.Info(tmpStr);
	         		}


I tried doing traversing through with this type of logic:
myList2[indexx + offSet2].MoveTo();
	         				myList2[indexx + offSet2].GetInnerHtml(); 
	         				now = System.DateTime.Parse(myList2[multi].GetInnerHtml());

Was This Post Helpful? 0
  • +
  • -

#8 regex  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 33
  • Joined: 25-September 12

Re: Date Format

Posted 05 October 2012 - 11:44 AM

Fixed it with this code:

public void DateFinalFirstRow()
        {
        	
        	 int indexx = -11;
        	 int indexxTmp = 0; 
        	 int offSet = 11;
        	 int offSet2 = 8; 
        	 int maxLoop = 0; 
        	 int multi = indexx + offSet2 - 1; 
        	 string found = string.Empty;
        	 string[] textStrArr = {"Created", "Incomplete", "Complete", "Cancelled Without QA"};
        	 string[] textStrArr2 = {};
        	 string[] textStrArr3 = {};
        	 string tmpStr = string.Empty;
        	 string differenceStr = string.Empty; 
        	 string differenceTotStr; 
        	 string dateStr;
        	 int differenceTot = 0; 
        	 var grid = repo.portal.buttons.Column_Headings.rgMasterTable;
        	 //var editQA = repo.portal.Order_Details.EditQA;
        	 //var viewQA = repo.portal.Order_Details.View;
        	 var date = System.DateTime.Parse("9/18/2012 2:45:44 PM");
        	 var now = System.DateTime.Now;
        	 var difference = now - date; 
        	 //Ranorex.DateTime = now - date; 
        	  
	         IList<TrTag> myList = grid.FindDescendants<Ranorex.TrTag>(); 
	         IList<TdTag> myList2 = grid.FindDescendants<Ranorex.TdTag>();

	         
	         
//	         foreach(TdTag tag in myList2 )
         	 {  

//	         		myList2[1].MoveTo(); 
//	         		myList2[1].PerformClick();
	         		
	         	maxLoop = (int)(myList2.Count / (offSet));
         		    
	         		for (int j = 0; j < maxLoop - 1; j++)
	         		{

//	         			indexxTmp = indexx / maxLoop;
				         
				        indexx += offSet;
				         			
				        myList2[indexx].MoveTo();
				         			
				        tmpStr = myList2[indexx].InnerText;
	         
	         			if(tmpStr == textStrArr[2].ToString())
	         			{
	         				//myList2[indexx + 8].MoveTo();
	         				dateStr = myList2[indexx + 8].InnerText;
	         				Report.Info(dateStr); 
	         				date = System.DateTime.Parse(dateStr);
	         				difference = now - date; 
	         				differenceStr = difference.ToString();
	        				differenceTot = Convert.ToInt32(difference.TotalDays);
	         				differenceTotStr = differenceTot.ToString(); 
	         				Console.WriteLine("There are {0:#.##} days between {1} and {2}.", difference.TotalDays, date, now);
	         				Report.Info(differenceStr); 
	         				Report.Info(differenceTotStr); 
	         				
			         		if(difference.TotalDays > 14)
			         		{
			         			Report.Failure("fail.  More than 14 days have passed and a completed order still remains"); 
			         		}
			         		if(difference.TotalDays <= 14)
			         		{
			         			Report.Success("The items are less than 14 days old.  Matching the requirement");
			         		}
			         		
			         		
			         		
			         	
	         			}
	         			
	         			
	         			Report.Info(tmpStr);
	         		}

This post has been edited by Curtis Rutland: 05 October 2012 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4468
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Date Format

Posted 05 October 2012 - 11:46 AM

Please stop quoting the previous posts. Only do that if you're replying to a post higher up in the thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1