Android Question [solved] Date problem when remote query SQL Server

makis_best

Well-Known Member
Licensed User
Longtime User
Hi

I use this tutorial trying to retrieve data remote from my database.
https://www.b4x.com/android/forum/threads/connect-android-to-ms-sql-server-tutorial.13166/
Everything is working fine and I can retrieve easy everything I want, but there is a problem when I use dates.....

I am using a simple query with a date field.
The query is SELECT RegistrationDate AS Date1 FROM Note1
So I test it in my browser like that
http://192.168.1.1/CONN.ASPX?QUERY=SELECT RegistrationDate AS Date1 FROM Note1
I save the result in a sqllite database

But I get a result like that....

[{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"},{"Date1":"\/Date(1498856400000)\/"}

Why? what I do wrong?
Is there any way I can solve that problem or the date is correct and I don't know what to do with the result?
 
Last edited:

keirS

Well-Known Member
Licensed User
Longtime User
That's a date value as ticks. So just use DateTime.Date() to get the string representation.
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
I understand.... but if i want to format it in query?
That means.... that if I want to store the date to sqlite it must
stored to a field with type long...
Correct?
 
Last edited:
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
I don't think that Date(1498856400000) is a tick value cuz every try I do I get crazy results.
I think that something is wrong with the base value.

I don't think it is tick value.

When I use this page http://tickstodatetime.com/ and I test 1498856400000 as value
doesn't return a date.
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The result you get in Log is not correct....
If I execute the query with order by RegistrationDate

The dates in query start from 07/01/2017
The difference could be time zone related. Have you tried the code to see what you get back?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I get 06/30/2017
And that is what it is.
Source: https://stackoverflow.com/questions/206384/how-do-i-format-a-microsoft-json-date
If you want something else returned, another format, then you need to change your ASPX page code. Note one of the link's comments:
So anyone with some ASPX chops may be able to help you out. But the result (the 1498856400000) comes directly from your database.
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The number (1498856400000) comes from some database that your are accessing via your ASPX page. Using this site (https://www.epochconverter.com/), 1498856400000 is
Assuming that this timestamp is in milliseconds:
GMT: Friday, June 30, 2017 9:00:00 PM
The date is the same for me, but the time is different (since I'm in a different timezone). So the date could be displayed differently if the viewer that you are using to view the database is in a different timezone or is configured for a different timezone.

BTW: http://tickstodatetime.com/ is based on
Source: https://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx

B4A DateTime is based on
Source: https://www.epochconverter.com/
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User

Which is why I asked what the column type was for the date. SQL server supports storing dates with and without timezone offsets.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
So what timezone are you in? Because the JSON serialization in .net will take a DATETIME(3) value as being at UTC 0 (no timezone offset). When it's serialized it serializes it to the local timezone of the browser.
 
Last edited:
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
My timezone is Athens (+3)

Until now I have try this options:
SELECT strftime('%Y-%m-%d', datetime((SUBSTR(RegistrationDate, 7, 13))/1000)/10000000 - 62135596800, 'unixepoch') AS Test,
SUBSTR(RegistrationDate, 7, 13) AS Test2,
datetime((SUBSTR(RegistrationDate, 7, 13))/10000000 - 62135596800, 'unixepoch') AS test3,
strftime('%Y-%m', datetime((SUBSTR(RegistrationDate, 7, 13))/10000000 - 62135596800, 'unixepoch')) AS test4,
datetime((SUBSTR(RegistrationDate, 7, 13))/10000000 - 62135596800, 'unixepoch') AS Test5,
RegistrationDate AS 'DAte' FROM ClientTable WHERE GID = '87e40e24-572b-4660-bbd6-747e6e83da79'


And the result I get Is that... all wrong...
 
Last edited:
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
I use this to set my timezone .
B4X:
DateTime.SetTimeZone(here your time zone mines -4)

when I am calculating dates.

Victor
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
I don't want to do the conversion from tick to date in vb.
I want to do the conversion on sqllite script.
There is no way?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…