# Convert a VB Date

**URL:** <https://forum.xojo.com/t/convert-a-vb-date/25533>\
**Category:** General\
**Created:** [July 16, 2015, 6:52am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533 "2015-07-16T06:52:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [July 16, 2015, 6:52am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/1 "2015-07-16T06:52:11Z")

</div>

I need to read a flat-file “database” created in VB6 and convert it to an SQLite database. The flat-file stores dates in the VB date data type, which was essentially a double, with the date to the left of the decimal point and time to the right of it.

I was wondering if anybody had already tackled this and perhaps had some conversion code for VB Date -\> Xojo (or SQL) Date?

---

<div class="post-metadata">

**Author:** ![shao\_sean](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@shao\_sean](https://forum.xojo.com/u/shao_sean)\
**Post date:** [July 16, 2015, 7:38am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/2 "2015-07-16T07:38:55Z")

</div>

Can you post a sample of the VB6 date format (what you would see back from the “database”)…

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [July 16, 2015, 7:42am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/3 "2015-07-16T07:42:08Z")

</div>

Here’s what it looks like: 42195.4519560185217415. It’s a Double (8 bytes) in the flat-file.

---

<div class="post-metadata">

**Author:** ![shao\_sean](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@shao\_sean](https://forum.xojo.com/u/shao_sean)\
**Post date:** [July 16, 2015, 8:14am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/4 "2015-07-16T08:14:01Z")

</div>

And what is the date and time of that?

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [July 16, 2015, 8:41am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/5 "2015-07-16T08:41:20Z")

</div>

Add that number of days to 01/01/1900 and you should be in the right ballpark.

`
Dim d As New Date(1900,1,1)
d.day = d.day + 42195.4519560185217415`

…should get you 11 July?

(the new framework uses .add for this incrementation, I gather…

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 16, 2015, 11:59am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/6 "2015-07-16T11:59:08Z")

</div>

> [@200775:@Jason Tait](#):
>
> 42195.4519560185217415

Must be some proprietary format. I am not aware of anything resembling that in VB6 date.

If we knew what it is supposed to represent, maybe some reverse engineering would be possible.

---

<div class="post-metadata">

**Author:** ![Louis\_D](https://forum.xojo.com/letter_avatar_proxy/v4/letter/l/cab0a1/32.png) [@Louis\_D](https://forum.xojo.com/u/Louis_D)\
**Post date:** [July 16, 2015, 2:43pm UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/7 "2015-07-16T14:43:50Z")

</div>

> [@](#):
>
> 42195.4519560185217415

It looks like a datetime stored as a double. I remember seeing something like it in MSAccess exports. Perhaps [this](https://support.microsoft.com/en-us/kb/210276) could be of help.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [July 16, 2015, 3:42pm UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/8 "2015-07-16T15:42:01Z")

</div>

The Access docs say 31/12/1899 = 1.0 so my 01/01/1900 is likely a day off.

`[code]Dim d As New Date(1900,1,1)
d.day = d.day + 42195.4519560185217415 - 1`[/code]

or

`Dim d As New Date(1899,12,31)
d.day = d.day + 42195.4519560185217415 `

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [July 17, 2015, 1:56am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/9 "2015-07-17T01:56:52Z")

</div>

Thank you to all who jumped in on this one. The VB double 42195.45195601852174 represents 10 July 2015 at 10:50:49 am.

I found [this site](http://ros.thevbzone.com/data_types_main.html) which, while last updated more than 15 years ago (!), covered the VB date issue in some detail. It turns out that VB dates start from 30 December 1899 so @Jeff Tullin you were pretty close! 🙂

Using Jeff’s code for inspiration plus the link above I have this code in a method converting VB dates (as doubles) to Xojo Date objects in the classic framework:

```auto
dim daysPart as double = Floor(vbDateTime)
dim timePart as double = (vbDateTime - daysPart) * 86400
        
dim newDate As New Date(1899,12,30)
newDate.day = newDate.day + daysPart
newDate.TotalSeconds = newDate.TotalSeconds + timePart
```

---

<div class="post-metadata">

**Author:** ![Jason\_Tait](https://forum.xojo.com/user_avatar/forum.xojo.com/jason_tait/32/5896_2.png) [@Jason\_Tait](https://forum.xojo.com/u/Jason_Tait)\
**Post date:** [September 8, 2015, 3:26am UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/10 "2015-09-08T03:26:15Z")

</div>

Just a further update to this to help anybody that might find it while searching for a similar solution.

While the code I posted above works fine on OS X, it doesn’t work under Windows (or at least it doesn’t work for me). Under Windows, the highest value for the vbDateTime double variable that works is 32,737 which creates the date 17 August 1989. But if vbDateTime is 32,738 the Xojo Date rolls back to 14 March 1810…

To fix it, I added code above the existing code to “seed” a different start date under Windows:

```auto
dim startYear as integer = 1899
dim startMonth as integer = 12
dim startDay as integer = 30
  
#if TargetWin32 then
    if vbDate > 32737 then
      vbDate = vbDate - 32737
      startYear = 1989
      startMonth = 8
      startDay = 17
    end
#endif
```

I also changed the way I construct newDate:

```auto
dim newDate As New Date(startYear, startMonth, startDay)
```

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:51pm UTC](https://forum.xojo.com/t/convert-a-vb-date/25533/11 "2020-10-29T17:51:01Z")

</div>


