Question

I have spent quite a few hours and still unable to understand this:

Dim unix_time_at_midnight As Long
DateTime.DateFormat = "MM/dd/yyyy"   
unix_time_at_midnight = DateTime.DateParse(DateTime.Date(unix_time*1000))/1000

where both unix_time_at_midnight and unix_time are long values. I understand DateTime.DateParse excepts a String and converts it to DateTime. What is (DateTime.Date(unix_time*1000))/1000 returning and what is its equivalent in Java? The requirement is to get the number of seconds since GMT midnight and I have successfully implemented it in Java. However, I would like to understand this particular line of code written in VB.net

EDIT: This method was written in Basic4Android and probably constitutes more of its libraries then vb.net. However, I have looked into each for details but unable to understand. Would appreciate if you could elaborate. Please see the links.

Was it helpful?

Solution

Take this:

DateTime.Date(unix_time*1000)

The documentation says:

Date (Ticks As Long) As String

Returns a string representation of the date (which is stored as ticks). The date format can be set with the DateFormat keyword.

So that part returns a string representing the date.

It then uses DateTime.DateParse, which is documented as:

DateParse (Date As String) As Long

Parses the given date string and returns its ticks representation.


Taken together, this appears to take the ticks, multiplied by 1000, converted to a string that doesn't contain hour information which is parsed back to ticks which are divided by 1000.

The important thing to note is that the DateFormat set on the line before contains only the formatting for the date, no hours/minutes/seconds and smaller units of time exist in it. This means that the string returned essentially represents midnight of that date.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top