Forum guidelines
AuthorQuestion
Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013
I am getting an error below that the string is not a valid datetime format. Can someone help below.

using System;
using System.Linq;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
       string[] ArrayElement = new string[6];
       ArrayElement[0]= "1:05:00 PM 06/25/2013 On True";
       ArrayElement[1]= "1:10:00 PM 06/25/2013 Off True";
       ArrayElement[2]= "1:15:00 PM 06/25/2013 On True";
       ArrayElement[3]= "1:20:00 PM 06/25/2013 Off True";
       ArrayElement[4]= "1:25:00 PM 06/25/2013 On True";
       ArrayElement[5]= "1:30:00 PM 06/25/2013 Off True";
 
       Console.WriteLine(Validate(ArrayElement)); // True  

       ArrayElement[2]= "1:15:00 PM 05/25/2013 On True";
       Console.WriteLine(Validate(ArrayElement)); // False  
       Console.ReadKey();  
    }

    static bool Validate(string[] state)
    {
       string f = "h:mm:ss tt MM/dd/yyyy";
 
       // extract time stamps and make sure they're in sequence
       DateTime[] stamps = state
       .Select(s => DateTime.ParseExact(s.Substring(0, s.LastIndexOf(" ")), f, null)).ToArray();
       for(int i = 1; i < stamps.Length; i++)
       {
           if(stamps[i] < stamps[i-1]) return false;
       }

       // extract On/Off's and validate them
       state = state.Select(s => s.Substring(s.IndexOf("O"))).ToArray();
       string text = String.Join("", state);
       string regExp = "^(OnOff)+$";
       return Regex.IsMatch(text, regExp);
    }
}

AuthorReply
Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 21 Aug 2013  
Try this:

using System;
using System.Linq;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
       string[] ArrayElement = new string[6];
       ArrayElement[0]= "1:05:00 PM 06/25/2013 On True";
       ArrayElement[1]= "1:10:00 PM 06/25/2013 Off True";
       ArrayElement[2]= "1:15:00 PM 06/25/2013 On True";
       ArrayElement[3]= "1:20:00 PM 06/25/2013 Off True";
       ArrayElement[4]= "1:25:00 PM 06/25/2013 On True";
       ArrayElement[5]= "1:30:00 PM 06/25/2013 Off True";
 
       Console.WriteLine(Validate(ArrayElement)); // True  

       ArrayElement[2]= "1:15:00 PM 05/25/2013 On True";
       Console.WriteLine(Validate(ArrayElement)); // False  
       Console.ReadKey();  
    }

    static bool Validate(string[] state)
    {
       string f = "h:mm:ss tt MM/dd/yyyy";
 
       // extract time stamps and make sure they're in sequence
       DateTime[] stamps = state
       .Select(s => DateTime.ParseExact(s.Substring(0, s.IndexOf("O") - 1), f, null)).ToArray();

       for(int i = 1; i < stamps.Length; i++)
       {
           if(stamps[i] < stamps[i-1])
           {
              return false;
           }
       }

       // extract On/Off's and validate them
       state = state.Select(s => Regex.Match(s, "(On|Off)").Value).ToArray();
       string text = String.Join("", state);
       string regExp = "^(OnOff)+$";
       return Regex.IsMatch(text, regExp);
    }
}

The output should be:

True
False


Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
Vuples I havent tested it yet. But is there another way to use the without using the for loop.

 

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
Vuples your logic is not accounting for the AM and PM in the timestamp

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
Vuples I still get the error String was not recognized as a valid DateTime.

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  

    string test = ArrayElement[0].Substring(0, ArrayElement[0].IndexOf("O") - 1);

This logic works below, which is for testing purposes.

                    string f = "h:mm:ss tt MM/dd/yyyy";

                   DateTime dt =  DateTime.ParseExact(test, f, null);

              Console.WriteLine(test);


This logic is not working at all below, I am getting string is not in a valid datetime format.

                      DateTime[] timestamps = ArrayElement.Select(s => DateTime.ParseExact(s.Substring(0, s.IndexOf("O") - 1), f, null)).ToArray();


Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 21 Aug 2013  
I've just tried it again to make absolutely sure but the code is working fine as posted.

The AM/PM desigmator is catered for in the format string you provided yourself:

string f = "h:mm:ss tt MM/dd/yyyy";

I suppose you could replace this part of the code:

       DateTime[] stamps = state
       .Select(s => DateTime.ParseExact(s.Substring(0, s.LastIndexOf(" ")), f, null)).ToArray();
       for(int i = 1; i < stamps.Length; i++)
       {
           if(stamps[i] < stamps[i-1]) return false;
       }

with this:

     var orderedState = state.OrderBy(s => DateTime.ParseExact(s.Substring(0, s.IndexOf("O") - 1), f, null));
     if(!state.SequenceEqual(orderedState)) return false;





Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
What exception class can I throw instead of returning.

Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 21 Aug 2013  
I'm not sure that any of the standard Exception classes really fit the bill.

I suppose ArgumentException is the closest as there's something wrong with the array that's passed in:

throw new ArgumentException("Array elements are not in order");

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
I am still getting the same error string was not recognized as a valid datetime.

This is my exact code below.

Also this is my exact string below when I look in the debugger.

"8/21/2013 10:05:09 On True", I notice that I dont have the AM/PM in the string. That could be the problem also

   private const string TIMESTAMP_FORMAT = "h:mm:ss tt MM/dd/yyyy";

   private static bool Validate(string[] state)
      {
                  var orderedState = state.OrderBy(s => DateTime.ParseExact(s.Substring(0, s.IndexOf("O") - 1), TIMESTAMP_FORMAT, null)); I get an error on this line.
                
 if (!history.SequenceEqual(orderedState))
                  {
                      return false;
                  }
else
{
   return true;
}
}

Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 21 Aug 2013  
If you don't have AM or PM in the string, then that's definitely the problem as you're using DateTime.ParseExact.


Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 21 Aug 2013  
Also I've just noticed that you've got the date before the time rather than the other way around.

The strings need to be in the exact format specified for this to work.

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
Vuples so do i need to make sure that AM/PM is the string like below.

string f = "MM/dd/yyyy h:mm:ss tt";

"8/21/2013 10:05:09 AM On True"

However, I dont think the substring is working correctly embedded in the ParseDate function below. I the error is complaining about the entire string "8/21/2013 10:05:09 AM On True". When you think "ON" and "TRUE" is an invalid  datetime format if that make sense.

         var orderedState = state.OrderBy(s => DateTime.ParseExact(s.Substring(0, s.IndexOf("O") - 1), TIMESTAMP_FORMAT, null)); I get an error on this line.
                
 if (!history.SequenceEqual(orderedState))
                  {
                      return false;
                  }


Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
Well thats not it. The substring is working. But however I am still getting the error. I am not for sure what it is.

 string [] ArrayElement = state.Select(s => s.Substring(0, s.IndexOf("O") - 1)).ToArray();

                  // extract time stamps and make sure they're in sequence
                  var orderedState = ArrayElement.OrderBy(s => DateTime.ParseExact(s, TIMESTAMP_FORMAT, null));

                  if (!history.SequenceEqual(orderedState))
                  {
                      return false;
                  }


Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 21 Aug 2013  
I don't know what 'history' is but you should be comparing the original array with the same array after it's been ordered to see whether they're the same or not:

                  if (!ArrayElement.SequenceEqual(orderedState))
                  {
                      return false;
                  }

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
That was a mistake. I still get the error when using Thr ArrayElement string arrat
Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 21 Aug 2013  
Another thing I've noticed is that we're expecting the date to be in this format:

MM/dd/yyyy

However, I've noticed one or two dates in your posts which only had one digit in the day.

Maybe we should be using M/d/yyyy instead?

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 21 Aug 2013  
Vuples this format work below.

   private const string TIMESTAMP_FORMAT = "M/d/yyyy HH:mm:ss tt";

what is the difference between "M/d/yyyy" and "MM/ds/yyyy"


Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 22 Aug 2013  
The difference is that you always have to use 2 digits to express the month or day with 'MM/dd/yyyy'.  So today would be 22/08/2013.

With 'M/d/yyyy' you use one digit if it's a single digit month or day or two digits otherwise. So today in this format would be 22/8/2013.

Personally, I prefer to use the 'MM/dd/yyyy' format because this always has the same length. However, if the data is being supplied externally, you may not have a choice. 

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 22 Aug 2013  
I prefer to use "MM/dd/yyyy" also. I am not for sure why this didn't work at all.

However this work "M/d/yyyy" I am very confuse why this work and this did not work "MM/dd/yyyy".

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 22 Aug 2013  
Can I go wrong using this format "M/d/yyyy"

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 22 Aug 2013  
Can I go wrong using this format "M/d/yyyy"

Re: Array Sequence TimeStamp Validation
Posted By Vulpes on 22 Aug 2013  
Well, "M/d/yyyy" would validate a date such as "8/1/2013" and also "08/01/2013".

So, if either of these are possible, I'd stick with this.

Re: Array Sequence TimeStamp Validation
Posted By David Smith on 22 Aug 2013  
I found out why the "MM/dd/yyyy" was not working. I look at the input string carefully and the input was incorrect according to the format.

9/25/2013 Incorrect

09/25/2013 correct

I am enduring the format is using the "MM/dd/yyyy" by doing this below.

sb.Append(c.ColorTimeTag.ToString("MM/dd/yyyy HH:mm:ss tt"));

Thanks vuples.

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.