0

Hello I am currently using a macro that is leveraged in both London and NY. There is a section in the macro that timestamps the action using =now this is creating a unique situation where actions we perform in NY look like they occurred before actions performed in London.

My question is are you able to convert =now to EST, as to create a uniform timestamp throughout the project?

for reference the line of actual code looks like this

mws.Range(Cells(Lastmwsr + 1, 2), Cells(Lastmwsr + 1, 2)).Value = Now

EDIT: Solutions that include adding +5 to the hour, still need to define how to recognize the time zone to call the argument as this has to be used in multiple regions.

Thanks,

Community
  • 1
  • 1
dom176
  • 187
  • 1
  • 15
  • 1
    Possible duplicate of [Show current time (EST)](https://stackoverflow.com/questions/33482717/show-current-time-est) – Pᴇʜ Jun 26 '18 at 12:19
  • 1
    You might want to take a look at [Get Timezone Information in VBA (Excel)](https://stackoverflow.com/questions/3120915/get-timezone-information-in-vba-excel) – Vincent G Jun 26 '18 at 12:20
  • 2
    If you want something additional to just adding an hour difference have a look at [Chip Pearsons page on timezones](http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx) – Tom Jun 26 '18 at 12:20
  • I read through Chip Pearson's page, admittedly its a bit above my skill level implementing that into my code. – dom176 Jun 26 '18 at 12:23
  • This looks also quite relevant - https://stackoverflow.com/questions/3120915/get-timezone-information-in-vba-excel – Vityata Jun 26 '18 at 12:39

3 Answers3

2

I read through Chip Pearson's page, admittedly its a bit above my skill level implementing that into my code.

http://www.cpearson.com/excel/TimeZoneAndDaylightTime.aspx

I think there's an error in the late Chips code - his ConvertLocalToGMT and GetLocalTimeFromGMT uses a variable called StartTime - I think in both cases this should be the variable that is being passed to the function.

To use his code you'd write something like:

Range("A1") = GetLocalTimeFromGMT(Now())  

or

Range("A1") = ConvertLocalToGMT(Now())

I've included the code from his site below. Just copy and paste into a new module.

Option Explicit

    Private Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
    End Type

    Private Type TIME_ZONE_INFORMATION
        Bias As Long
        StandardName(0 To 31) As Integer
        StandardDate As SYSTEMTIME
        StandardBias As Long
        DaylightName(0 To 31) As Integer
        DaylightDate As SYSTEMTIME
        DaylightBias As Long
    End Type

    Private Enum TIME_ZONE
        TIME_ZONE_ID_INVALID = 0        ' Cannot determine DST
        TIME_ZONE_STANDARD = 1          ' Standard Time, not Daylight
        TIME_ZONE_DAYLIGHT = 2          ' Daylight Time, not Standard
    End Enum

    Private Declare Function GetTimeZoneInformation Lib "kernel32" _
        (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

    Private Declare Sub GetSystemTime Lib "kernel32" _
        (lpSystemTime As SYSTEMTIME)


    Function ConvertLocalToGMT(Optional LocalTime As Date) As Date
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' ConvertLocalToGMT
    ' This function returns the GMT based on LocalTime, if provided.
    ' If LocalTime is not equal to 0, the GMT corresponding to LocalTime
    ' is returned. If LocalTime is 0, the GMT corresponding to the local
    ' time is returned. Since GMT isn't affected by DST, we need to
    ' subtract 1 hour if we are presently in GMT.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim T As Date
        Dim TZI As TIME_ZONE_INFORMATION
        Dim DST As TIME_ZONE
        Dim GMT As Date

        If LocalTime <= 0 Then
            T = Now
        Else
            T = LocalTime
        End If
        DST = GetTimeZoneInformation(TZI)
        GMT = T + TimeSerial(0, TZI.Bias, 0) - IIf(DST = TIME_ZONE_DAYLIGHT, TimeSerial(1, 0, 0), 0)
        ConvertLocalToGMT = GMT

    End Function

    Function GetLocalTimeFromGMT(Optional GMTTime As Date) As Date
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' GetLocalTimeFromGMT
    ' This returns the Local Time from a GMT time. If GMTTime is present and
    ' greater than 0, it is assumed to be the GMT from which we will calculate
    ' Local Time. If GMTTime is 0 or omitted, it is assumed to be the GMT
    ' time.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim GMT As Date
        Dim TZI As TIME_ZONE_INFORMATION
        Dim DST As TIME_ZONE
        Dim LocalTime As Date

        If GMTTime <= 0 Then
            GMT = Now
        Else
            GMT = GMTTime
        End If
        DST = GetTimeZoneInformation(TZI)
        LocalTime = GMT - TimeSerial(0, TZI.Bias, 0) + IIf(DST = TIME_ZONE_DAYLIGHT, TimeSerial(1, 0, 0), 0)
        GetLocalTimeFromGMT = LocalTime

    End Function
Darren Bartrup-Cook
  • 18,362
  • 1
  • 23
  • 45
  • Thanks Darren, appreciate you diving into that a bit. My biggest issue working with Chips code was troubleshooting through some of the error that were popping up. For instance I have a compile error at: Private Type TIME_ZONE_INFORMATION. Not quite sure how to work through it. – dom176 Jun 26 '18 at 13:01
  • All the code before `Function ConvertLocalToGMT(Optional LocalTime As Date) As Date` should appear at the very top of any module you paste it into. If you have any procedures/functions before that you'll get `Compile error: Only comments may appear after End Sub, End Function, or End Property`. – Darren Bartrup-Cook Jun 26 '18 at 13:04
  • Although the link provided by @Vityata looks very promising - the answer given by Patrick Honorez looks the easiest to follow. – Darren Bartrup-Cook Jun 26 '18 at 13:08
  • Thanks Darren I was able to implement that pretty easily. Appreciate you working through it. – dom176 Jun 26 '18 at 13:23
  • Check _it is_ returning the correct time - I'm on GMT so not sure what result it will give from other time zones. Any problems will more than likely come from `DST = GetTimeZoneInformation(TZI)` where it's calculating daylight savings. – Darren Bartrup-Cook Jun 26 '18 at 13:31
0

This is a way to display the value of Now adding 5 hours to it. You may consider a function as well.

Public Sub TestMe()

    Dim fixTime As Long
    fixTime = 5
    Range("a1") = TimeSerial(Hour(Now) + fixTime, Minute(Now), Second(Now))

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • 1
    That wouldn't work because while that would bring London in line with NY. I would have to create a separate function for NY. – dom176 Jun 26 '18 at 12:20
  • @dom176 and why exactly should creating a seperate function be an issue? In fact, you could create a direct argument to pass the time difference for timezone inside the function! – Samuel Hulla Jun 26 '18 at 12:25
  • 1
    @Rawrplus even still you need to determine the timezone, so you can either call that argument or ignore it. You can't just directly add 5 by default. – dom176 Jun 26 '18 at 12:28
  • @dom176 Well... the easiest solution I can think of is sadly to create an ordered list (probably through [`Select Case`](https://www.techonthenet.com/excel/formulas/case.php) - known as switch in most languages - which would contain list of cities and their actual timezones (or how much to add, to be specific). I don't think there is anything in excel implemented for handling specific time zones - though if you scour the internet you might find something, for sure somebody made some sort of data-table already – Samuel Hulla Jun 26 '18 at 12:31
0

I had to find a similar solution for a workflow system where the long answer from Get Timezone Information in VBA (Excel) could not be used (too long code).

I also had to format the date, so I combined the functions Format and DateAdd:

Format(DateAdd("h", 5, Now), "dd.MM.yyyy HH:mm:ss")

Some explanation

  • Now returns the current system date and time.
  • "h" specifies that the hours from the returned date and time (Now) have to be calculated up by "5".
  • "dd.MM.yyyy HH:mm:ss" specifies the syntax output.

For example the output of the date and time: 28.07.2020 00:42:09, would be 28.07.2020 05:42:09.

This solution considers leap years as well.

I know this is a dirty solution and not the perfect answer for the question, but maybe it helps somebody anyways.

Further documentary: