How to insert UTC Time into a cell

rskip

New Member
Joined
Nov 26, 2009
Messages
46
Office Version
  1. 365
Platform
  1. Windows
I need a cell with UTC Time so as I can reference to other cells with different time zones. I know how to figure the different time zone time from UTC. So do I need VBA to do that, and what is the code. It would be great if it auto updated also.
Thanks using Office 365.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
After searching Google, I came across the following link in which it describes how to get the current UTC date and time...


Accordingly, first add the following custom function to a regular module...

VBA Code:
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

Then you can enter the following worksheet formula to get the current UTC date and time...

Excel Formula:
=GimmeUTC()

Hope this helps!
 
Upvote 0
You can add Application.Volatile at the beginning of the custom function...

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

Now the worksheet formula will automatically re-calculate whenever there's a change in any cell within the workbook, or you calculate the sheet using Shift+F9, or you calculate the workbook using F9.
 
Upvote 0
I must be doing something wrong as it does not refresh even using shift +F9
 
Upvote 0
I'm assuming that you've added Application.Volatile to the code, correct?

How did you format your cell?

You should be able to update the formula by using Shift+F9 if the sheet containing the formula is the active sheet, or by using F9 to calculate the entire workbook.
 
Upvote 0
Solution
Thanks I formatted the cell on a different tab, so when I press F9 on that tab, all is good, thanks for your help!!!
 
Upvote 0
This works perfect, and thank you. As a nit: I have a cell formatted in Z time as: yyyy-mm-ddThh:mm:ss.000Z

GimmeUTC does not offer the added decimal precision.
=now() local 2024-03-14T09:19:05.980Z (yeah, the Z is forced by the format, but this is local time)
=gimmeUTC() 2024-03-14T14:19:05.000Z (and while the hrs are adjusted to Z, thank you, hrs, minutes, but no decimal)

The question: is there an easy way to "add" my missing decimal seconds to gimmeUTC?

I've run down the rabbit hole of trying to truncate, or add, and haven't stumbled on a combination that works.

Just thought it odd that the decimal seconds is not provided despite being formatted the same.

drW

(PS, you are one of my go-to for expert Excel advice, thank you)
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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