User Defined Function for UTC doesn't display decimal seconds

drWireMORE

New Member
Joined
Jul 30, 2015
Messages
6
This is a working UDF, and I see at least two people have offered this solution, so kudos to both GimmeUTC() and UTC() in a windows environoment

Function GimmeUTC()
' Returns current date/time at UTC-GMT as an Excel serial date value
Dim dt As Object
Set dt = CreateObject("WbemScripting.SWbemDateTime")
dt.SetVarDate Now
GimmeUTC = dt.GetVarDate(False)
End Function

However, while now() displays with decimal seconds, the above does not. And yes the format is the same.

I have a non-elegant method which fixed that by stripping the decimal seconds with an associated now() formula and some math.

Question: Is there a way to get a UDF format as above to just include decimal seconds, or is that a limitation of the Microsoft object?

My non-elegant solution: Yeah, I could simplify some of the math, but this made it more apparent.

=GimmeUTC()+(((NOW()-TRUNC(NOW()))*24*60*60) -TRUNC((NOW()-TRUNC(NOW()))*24*60*60))/(24*60*60)
strips all but the decimal (hrs,min,sec) strips all but the decimal seconds and then calibrates it back to decimal seconds
yes, one could simpify 24*60*60 but this makes it apparent that converting fraction of a day into seconds by x (24) hrs x (60) minutes/hr x to (60)seconds/min
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Since the function returns a date you can format the cell to display fractional seconds, but the problem is that I do not believe the VBA NOW function will return anything but whole seconds. You might consider using a vba function to instead calculate the UTC offset, and use that with the Now() function to get the resolution you need.

Book1
AB
1TimeUTC Time
201:14:13.460 PM07:14:13.460 PM
Sheet6
Cell Formulas
RangeFormula
A2A2=NOW()
B2B2=NOW()+UTC_Offset()


VBA Code:
Function UTC_Offset() As Double
    With CreateObject("WbemScripting.SWbemDateTime")
        .SetVarDate Now
        UTC_Offset = .GetVarDate(False) - Now
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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