Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current datetime in UTC from an Excel VBA macro

Is there a way in an Excel VBA macro to get the current datetime in UTC format?

I can call Now() to get the current time in the local timezone; is there a generic way to then convert this to UTC?

like image 781
Jon Avatar asked Oct 21 '09 13:10

Jon


2 Answers

Simply, you can use COM Object to achieve UTC Time Information.

Dim dt As Object, utc As Date
Set dt = CreateObject("WbemScripting.SWbemDateTime")
dt.SetVarDate Now
utc = dt.GetVarDate(False)
like image 82
gogeek Avatar answered Sep 19 '22 16:09

gogeek


http://excel.tips.net/Pages/T002185_Automatically_Converting_to_GMT.html

There is a macro on that page with a LocalTimeToUTC method. Looks like it would do the trick. Also some formula examples if you wanted to go that route.

Edit - Another link. http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx This page has several methods for date/time. Pick your poison. Either should do the trick, but I feel like the second is prettier. ;)

like image 30
brad.huffman Avatar answered Sep 17 '22 16:09

brad.huffman