Home » excel » excel – Get date pattern yyyy-MM-dd HH:mm:ss z with VBA

excel – Get date pattern yyyy-MM-dd HH:mm:ss z with VBA

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m trying to get this Date Pattern yyyy-MM-dd HH:mm:ss z with VBA but can’t find any solution.

I’m getting the Time in input from a Excel cell or with Now Function and then formatting it with Format function, but did not find any format that gives me also Timezone information.

Format(Now, "yyyy-dd-MM HH:mm:ss")

Thanks for your help!

How to&Answers:

A date/time field in Excel does not contain any time zone information. Its numeric representation is an “OLE Automation Date”, which according to MSDN is defined as:

An OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899, and whose fractional component represents the time on that day divided by 24. For example, midnight, 31 December 1899 is represented by 1.0; 6 A.M., 1 January 1900 is represented by 2.25; midnight, 29 December 1899 is represented by -1.0; and 6 A.M., 29 December 1899 is represented by -1.25.

Nowhere in this definition does it say what the time zone reference is. It’s not necessarily the local time zone of the computer where you’re viewing the file, because you can move the file to a computer in another time zone and the data does not change. Nor is it necessarily based on UTC. At least, there’s nothing explicitly defining the data as UTC. You could just as well be showing local date/time values as UTC date/time values with the same representation.

Therefore, only you, as the owner of the Excel spreadsheet, can determine what the data in a given date/time field means with regard to time zone. If you know the fields contain UTC timestamps, then just add a character Z to the output string. If you know the fields are fixed to UTC+2, then add +02:00 to the output string. If you know only that the fields are in “Pacific Time”, well, then you have a problem because you’ll have to decide whether they are in PST (-08:00) or PDT (-07:00). You could use a formula or external function for that, but there’s nothing built in, and there’s nothing to tell you the field is in Pacific Time at all – other than your own external knowledge.