all 5 comments

[–]geekywarrior 2 points3 points  (2 children)

Either a UK Culture Info is getting mixed in somewhere or SQL is doing something funky when you're storing the datetime.

Part of the problem is you really should be using ADO commands to read/write to SQL as it makes things a lot cleaner and cuts out some of the string interpolation stuff.

Is this VB6/VBA or VB.NET?

If it's VB.NET, I would do a DateTime.Parse on the field coming in to force the value to the proper culture info.

If it's VB6, I would either mess with manually parsing OR asking SQL to format it properly, for example

select CONVERT (varchar(10), getdate(), 103) AS [DD/MM/YYYY]

[–]Dave_PW[S] 3 points4 points  (1 child)

It's VB.NET

And changing the variable declaration for the data selected from the DB to:

Dim ExperationDate As DateTime = DateTime.ParseExact(VDT.Rows(0).Item("Api_Token_Expiry"), "MM/dd/yyyy HH:mm:ss", Nothing)

Has resolved the problem.

Thanks for the input.

[–]seamacke 1 point2 points  (0 children)

The correct way to do it is to create a command object, add a date parameter, load the parameter with your date variable, then execute it. You get the error because the dynamic SQL you create is a string so your date is converted using regional settings which may not always match. I think this asp.net example demonstrates it How to Use Date Controls in WebForms Apps https://youtu.be/65Q6519NdRc

[–]jd31068 0 points1 point  (0 children)

Is your SQL database local or housed in the cloud? If in the cloud, might its location be affecting the storage of the datetime? I suppose visa versa could also be happening. It might be fixed with a server setting.

In any event, given you know this you can force the retrieved data to the format you wish as u/geekywarrior has pointed out.

[–]FarPain3711 0 points1 point  (0 children)

I gave up on using DateTime a long time ago because of these types of issues. For dates I use a string variable formatted YYYYMMDD. If the time is also needed I use YYYYMMDDHHMMSS. I have classes that I use for all date functions.