String to date time

AussieBoy

Well-known member
Joined
Sep 7, 2020
Messages
78
Programming Experience
Beginner
Hi,
I am trying to use

C#:
His.Interface.LoadDataValue(4, 192, DateTime.Now, csvList[4]);
The code above works. Of course the DateTime value is the current date time.

Instead of DateTime.Now I want to base the DateTime value from a string, from a column of a csv file csvList[1].
Example "25/06/2020 7:50:55.000 AM".

Do I need to some how convert a string to a long (which I don't know if this can be done).

Thanks,
 
The DateTime struct has a Parse() method to convert a string into a DateTime.

It's unclear what that long that you seek represents.
 
Hmmm... Didn't you already ask this question about converting a string to a date time?

 
The code you have is irrelevant. Whether you use DateTime.Now or not, that code needs you to pass it a DateTime value. Your problem is getting a DateTime value from a string. What you intend to do with that DateTime value is irrelevant to how you go about getting it.

Getting a DateTime value from a string is simple and you've already been show how to do it, so why are you asking again? The code in post #4 makes absolutely no attempt to use the code or principle provided in your previous thread so what's the problem? If csvList[1] is your string then you just put that into the code you were already provided with and out comes the DateTime you want.
C#:
if (DateTime.TryParseExact(csvList[1], "yyyy,MM,dd,HH,mm,ss", null, DateTimeStyles.None, out var value))
{
    // Your code here to use value, which is the DateTime.
}
else
{
    // The input was invalid so do as appropriate.
}
You can change the date/time format if required and you can do whatever you want with the DateTime that code produces. If that is pass it to that LoadDataValue method then do that.
 
Hi, I have made small changes. It doesn't through an error nor does it work
It did work with the DateTime.Now.
Thanks,
C#:
if (DateTime.TryParseExact(csvList[1], "dd/MM/yyyy HH:mm:ss.fff tt", null, DateTimeStyles.None, out var value))
                        {
                            // Your code here to use value, which is the DateTime.
                            His.Interface.LoadDataValue(4, 192, value, csvList[4]);
                        }
                        else
                        {
                            // The input was invalid so do as appropriate.
                        }
 
It doesn't through an error nor does it work
Of course it works, i.e. it does exactly what it should. The fact that you haven't used it properly doesn't mean that it's not working. Did you actually debug your code to see what it actually did? I'd wager not.

The issue is the fact that you have provided a nonsensical format specifier. You have specified "HH" for the hour, which means that the input must include two digits for the hour and will be interpreted as 24-hour time. Does your data contain two digits for the hour? Based on post #1, at least some it does not, so why would it parse successfully?

Based on the example data and the fact that you're using "tt" in the format specifier, your data is not in 24-hour format, so don't try to parse it as such. You are using 12-hour format and you are not padding single digit hours. That means that your format specifier should be "dd/MM/yyyy h:mm:ss.fff tt" rather than "dd/MM/yyyy HH:mm:ss.fff tt". You should already know that because you should have taken the time to read about data/time format specifiers and worked out what was the right one to use.
 
Back
Top Bottom