Fadi
Fadi

Reputation: 3322

Converting date to number

I have two original files and i use encryption application to encrypt them, this application change the last modified date of these files (like 7zip do), so i tried to save the original file's last modified date to a text file, so i can later read this date from the text file and compare it to see which file is newer before overwrite it.

But I don't want to use date and time data type, i want to convert it to number and the only way i know is to convert it to double but it failed.

So I'm looking for a way to save date as number then compare this number (if i could) or reconvert it to date to know which file is newer, i'm not looking for converting date to double then double to string.

Currently I get the file's last modified date using .LastWriteTime and convert it to double using .ToOADate but it didn't give a correct results.

I tried to simplify the problem in this code:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim d1 As Double, d2 As Double

        d1 = IO.File.GetLastWriteTime("D:\g2.xlsb").ToOADate                
        d2 = 42816.7630876736 

        Debug.Print(d1)      '42816.7630876736
        Debug.Print(d2)      '42816.7630876736
        Debug.Print(d1 = d2) 'False 

    End Sub

When I debug this code line by line and put the mouse one d1 its show a number with 12 decimal 42816.763087673608 but when i save it to a text file its save a number with only 10 decimal 42816.7630876736.

My question is: how to convert a date to a number and save it to text file then (later) read this number from this text file and recreate a date from it.

Thanks to @Steve for his answer (in comments),DateTime.Ticks is a great solution for me to avoid using double and its endless problems.

And I'm Sorry about the previous title (I edited it).

Upvotes: 1

Views: 2048

Answers (1)

Steve
Steve

Reputation: 216312

As you can read in the DateTime docs

Time values are measured in 100-nanosecond units called ticks

and obviously the DateTime structure has a property of type Long named Ticks.

This seems the perfect candidate for your requirements. Using Ticks you will be able to store the string representation of the date Ticks in a text file or write the long value in its binary form on a binary file. In any case you will not have precision problems from the long integer value and of course, you can reload the value from your storage.

Moreover the Ticks value can be used to recreate the DateTime variable and use it to check with the LastWriteTime (or LastWriteTimeUtc) of the file, while this cannot be done with a double value.

So for example (assuming the presence of a method called GetTicksFromStorage that returns the long ticks value stored somewhere)

 Dim ticks as Long = GetTicksFromStorage()
 Dim curTicks = IO.File.GetLastWriteTime("D:\g2.xlsb").Ticks

 If ticks < curTicks Then
     Console.WriteLine("G2 is newer")
 Else
     ....
 End If

or

 ' Here the result of the GetTicksFromStorage is passed to the DateTime constructor
 Dim storedDate as DateTime = new DateTime(GetTicksFromStorage())
 Dim curDate = IO.File.GetLastWriteTime("D:\g2.xlsb")
 if storedDate < curDate Then
     Console.WriteLine("G2 is newer")
 Else 
     ....
 End If        

Upvotes: 3

Related Questions