Convert# of seconds since a date that is before 1990?

im2bz2p345

Board Regular
Joined
Mar 31, 2008
Messages
229
Hi,

Is there a way to convert a text field that is formatted in seconds from the date of 1/1/1841 to mm-dd-yyyy h:mm:ss AM/PM?

For example, in cell F6, I have the text string 5476694198, which after conversion should output to Jul 19, 2014 3:56 PM

I found this article which discussing working with dates before 1/1/1990 in Excel: Pre-1900 Dates in Excel

Since the XDATE add-in created by John Walkenbach seems to be inaccessible now, I looked at the macro referenced (How to calculate ages before 1/1/1900 in Excel - Office), but is there a way to hardcode the end date and uses a formula that only references?

I'm using the latest Excel 365, 32-bit version if that makes a difference.

Regards,

~ im2bz2p345 :)
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
CORRECTION on first sentence: Is there a way to convert a text field string that displays the number of seconds from the date 1/1/1841 to a format such as mm-dd-yyyy h:mm:ss AM/PM?

~ im2bz2p345 :)
 
Upvote 0
Here's a UDF that ignores leap seconds (which were introduced in 1972). So for seconds elapsed that will end up later than 1972 this will overstate the time by a few seconds (less than 30). It uses a less than efficient algorithm that runs through each day updating the month, day, and year as it goes. For a one-time calculation it is almost immediate (.01 seconds or so) but if you are running many of these calculations at the same time, I wouldn't recommend using this.

VBA Code:
Function DateSeconds(FromYear As Integer, FromMonth As Integer, FromDay As Integer, seconds As Double) As String
    Dim Days As Double, i As Long, monthdays, months, dt As String, tm As String, t As Double
    t = Timer
    monthdays = Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31)
    months = Array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    Days = seconds / 24 / 3600
    For i = 1 To Int(Days)
        FromDay = FromDay + 1
        If FromDay > monthdays(FromMonth) Then
            If FromMonth <> 2 Or FromYear Mod 4 <> 0 Then
                FromDay = 1
                FromMonth = FromMonth + 1
                If FromMonth = 13 Then
                    FromMonth = 1
                    FromYear = FromYear + 1
                End If
            Else
                If FromDay = 30 Then
                    FromDay = 1
                    FromMonth = 3
                End If
            End If
        End If

    Next
    dt = months(FromMonth) & " " & FromDay & ", " & FromYear
    tm = Format(Days - Int(Days), "Long Time")
    DateSeconds = dt & " " & tm
    Debug.Print Timer - t
End Function

=DateSeconds(1841,1,1,5476694198) => Jul 19, 2014 3:56:38 PM
 
Upvote 0
Solution
Thank you BOTH for the replies.

Small correction. The start date is actually 12/31/1840 at midnight. See below for a detailed breakdown.

How did you calculate Jul 19 2014 ?
Consider the following...

Hi @Dave Patton - thanks for the reply, but unfortunately that won't work unless all it can be combined into one massive formula without using extra cells. I have a small sample export of data out of a system that using.

Here is how it is calculated manually:

RMR Transition Utility Export POC 1-4-2022.xlsx
FGH
65476694198# days = Total Seconds since 12/31/1840 (F6)/# of seconds in a day (86,400)63387.66433
7# of hours = 24 hours in a day X days in the remainder (.66433)15.94392
8# of minutes = 60 minutes in a hour X hours in the remainder (0.94392)56.6352
9# of seconds = 60 seconds in a minutes X minutes in the remainder (0.6352)38.112
HRX export
Cell Formulas
RangeFormula
H6H6=F6/86400
H7H7=24*0.66433
H8H8=60*0.94392
H9H9=60*0.6352


Values in H6, H7, H8, and H9 were plugged into this website: Date Calculator: Add to or Subtract From a Date – Results

Screenshot:

TimeDateCalculation.png


Thanks,

~ im2bz2p345 :)
 
Upvote 0
Here's a UDF that ignores leap seconds (which were introduced in 1972). So for seconds elapsed that will end up later than 1972 this will overstate the time by a few seconds (less than 30). It uses a less than efficient algorithm that runs through each day updating the month, day, and year as it goes. For a one-time calculation it is almost immediate (.01 seconds or so) but if you are running many of these calculations at the same time, I wouldn't recommend using this.

Hi @JGordon11. This is incredible and exactly what I was after. Thank you for putting it together.

I spot checked using your UDF with many other values and it outputted the correct value each time. I also copy-pasted it to several lines (35) and it almost instantly calculated, so it's definitely operating fairly efficiently.

I wanted to thank you for your efforts. I will wait a day for any other feedback, but will likely mark your response as the solution soon.

~ im2bz2p345 :)
 
Upvote 0
Thought about this and found the VBA Dateserial function can handle this. Interestingly it returned an answer one day later than the function I provided earlier. It is right and my previous function is off by a day. Reason is that years that are divisible by 100 but not by 400 have no leap day. So, for example, 1800 & 1900 had no leap day but 2000 did. So here is a UDF that is much shorter, faster and best of all it is correct for the century years leap day complication. It presumably works for years 100 to 9999.

VBA Code:
Function DateSeconds(FromYear As Integer, FromMonth As Integer, FromDay As Integer, seconds As Double) As String
    Dim dt As Double
    dt = DateSerial(FromYear, FromMonth, FromDay) + seconds / 24 / 3600
    DateSeconds = Format(dt, "mmm d, yyyy") & " " & Format(dt, "Long Time")
End Function
 
Upvote 0
Thought about this and found the VBA Dateserial function can handle this. Interestingly it returned an answer one day later than the function I provided earlier. It is right and my previous function is off by a day. Reason is that years that are divisible by 100 but not by 400 have no leap day. So, for example, 1800 & 1900 had no leap day but 2000 did. So here is a UDF that is much shorter, faster and best of all it is correct for the century years leap day complication. It presumably works for years 100 to 9999.

VBA Code:
Function DateSeconds(FromYear As Integer, FromMonth As Integer, FromDay As Integer, seconds As Double) As String
    Dim dt As Double
    dt = DateSerial(FromYear, FromMonth, FromDay) + seconds / 24 / 3600
    DateSeconds = Format(dt, "mmm d, yyyy") & " " & Format(dt, "Long Time")
End Function

Thank for the follow-up, research you did, and the improvements you made @JGordon11!

It works perfectly if I use the start date of midnight on 12/31/1840 as mentioned at the top of post #5, which is the correct starting date timestamp - unfortunately I could not update my original post, so apologies if any confusion was caused.

Another example that I tested out using the string 5460625709. =DateSeconds(1840,12,31,5460625709) => Jan 14, 2014 4:28:29 PM, which is correct.

The updated UDF does seem to operate/compute much more efficiently, as you mentioned.

I hope to use this UDF code with the function =DateSeconds(1840,12,31,<insert cell reference to # of seconds string>) going forward in all of spreadsheets. Once again, I really appreciate your efforts.

~ im2bz2p345 :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top