How can I convert SQL time using a datetime stamp to milliseconds. I am doing a highstock chart using VB.Net
and this is my selection code so far
Dim mydatalist As New List(Of XyData)
Dim sqlReader As SqlDataReader
Dim strConn As New SqlConnection(ConfigurationManager.ConnectionStrings("myCipConnection").ConnectionString)
Dim strSql As String = "SELECT TOP 1000 DATEDIFF(second,{d '1970-01- 01'},dtmStamp)AS dtmStamp ,dblReturnTemp FROM tblCIPEventData "
Dim sqlCmd As New SqlCommand(strSql, strConn)
strConn.Open()
sqlReader = sqlCmd.ExecuteReader
I have a SQL selection
Dim strSql As String = "SELECT TOP 1 dtmStamp ,dblReturnTemp FROM tblCIPEventData"
The result looks like
2009-10-22 11:29:31.513
How can I convert this to milliseconds
http://support.microsoft.com/kb/186265
You can use the SQL Server DATEPART() function to get the milliseconds of a SQL Server datetime field returned to a Visual Basic application.
Example:
SELECT Pubdate, DATEPART(Ms, Pubdate) FROM Titles
For work we did using a timestamp that was in the form of DATETIME we did the following because we were not concerned with the time of day:
CONVERT(BIGINT, DATEDIFF(s, '19700101', left(a.mdate, 11)))*1000
The left(a.mdate, 11)
gives data like = Apr 25 2012 (in readable format)
a.mdate = 2012-04-25 11:01:18.030
This is in the javascript time needed.
To figure out the milliseconds of a date you first have to compare it to some other DateTime. By subtracting two DateTime objects you will end up with a TimeSpan object. Then that TimeSpan object can be converted to milliseconds. This can all be done with one simple line of code:
Dim milliseconds As Double
milliseconds = (end - start).TotalMilliseconds
If you want to do it SQL you can just use Robert Harvey's answer
If you want to it in VB.NET and you have the following
Dim strSql As String = "SELECT TOP 1 dtmStamp ,dblReturnTemp FROM tblCIPEventData"
The result will be a System.DateTime
To answer the question How can I convert this to milliseconds? we can make use of TimeSpan
Dim dtmStamp as DateTime
dtmStamp = sqlReader.GetDateTime(0)
Dim ts As New TimeSpan(dtmStamp.Ticks)
Dim milliseconds as Long
milliseconds = TotalMilliseconds
This will give you the number of Milliseconds since 1/1/0001 12:00 AM
If you want the epoch to be 01/01/1970 you just do
Dim epoch as DateTime = new DateTime(1970,1,1)
Dim ts As New TimeSpan((dtmStamp - epoch).Ticks)
This did the job for me:
me.labelDisplay.Text = CDate(query.dateColumn).ToString("yyyy-MM-dd HH:mm:ss.fff")
Output:
2017-03-23 16:35:08.237
In case you want to get the milliseconds part use:
Dim ms as Integer = CDate(query.dateColumn).Milliseconds()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.