Hello Dojo Community !
I'm currently working on a C# script where I need to parse a string into a DateTime object. I've tried using DateTime.TryParseExact
with multiple date formats, but I'm facing issues identifying the correct format.
Code snippet :
LastRestart format on the cube (table) UI:
LastRestart value from the streamviwer :
Despite trying various formats, I'm still encountering issues with certain date strings. I am not sure if the format is the one observed in the table (UI) or the one from seen from the stream viewer (loggs). I'm looking for suggestions or guidance on how to dynamically identify the correct DateTime format from a given string.
Any help, insights, or alternative approaches to accomplish this task would be greatly appreciated!
Thank you in advance for your assistance.
As this question has now been open for a long time and there has been no further reaction from you, I will now close it. If you do wish to investigate this further, could you post a new question?
Yes , this can be closed now. Thank you
Hi Sidddique,
Let's try to separate the parsing from the visualization to isolate potential problems.
For the best visualization, use the following parameter options: options attribute | DataMiner Docs, and set your parameter using the DateTime.ToOADate method. Ideally, your parameter interprete is configured for doubles. This lets the parameter only accept double values, and is also the most efficient configuration when storing double values.
<Interprete>
<RawType>double</RawType>
<Type>double</Type>
<LengthType>fixed</LengthType>
<Length>8</Length>
<Decimals>8</Decimals>
</Interprete>
It's hard to tell what format you should pick if you only have one example, but the custom format not working in some cases. By default, the DateTime.Parse already tries a lot of standard formats. Yours seems to be "O" format ("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK"), but without the sub-second values. So the matching custom format is probably "yyyy-MM-dd'T'HH:mm:ssK". The dashes don't need to be single-quote escaped in my experience.
I suggest logging cases that fail to be parsed, and what the input was, so the format(s) can be corrected if necessary.
Hi,
If you are reading a parameter that is of type double, but marked as a datetime for display, then the string format depends on the local culture (of the system user, which runs the script and automation processes). In that case, you may be better off reading the double value and formatting it to a DateTime using FromOADate: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.fromoadate?view=net-8.0
Thank you Floris for your answer , I have replied to your suggestion with a different answer.
Hi Floris ,
Thank you for your suggestion , I am modifying the script to look into it and testing it out to observe results.
The current protocol snippet that shows the Date time :
with my script I would want collect datetime information using Getparameter method, then perform some calculation. But the formats options I had seemed to provide was not able to identify that parameter.
NB: reply to the previous answer does not allow me to post the screenshot, so i have added as a new answer .
Hi,
If you want to convert the value from that parameter to DateTime you should use DateTime.FromOADate instead of DataTime.TryParseExact.
DateTime.TryParseExact is when you have a string representation of a date, but in this case you have an OLE Automation date.
Hi,
I see that this question has been inactive for some time. Have you been able to find a solution for this yet? If yes, could you select the most helpful answer below, or add an answer of your own with the solution for the reference of other users who may have a similar problem?