Skip to content

Instantly share code, notes, and snippets.

@PhilippMundhenk
Last active March 7, 2017 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilippMundhenk/17e780e2cbcdc054e48a3ad3e5faa13a to your computer and use it in GitHub Desktop.
Save PhilippMundhenk/17e780e2cbcdc054e48a3ad3e5faa13a to your computer and use it in GitHub Desktop.
In Excel, the date format depends on the locale setting of the computer, e.g. the formula =TEXT(TODAY(),"YYYY-MM-DD") only works correctly on computers set to English locales, and =TEXT(HEUTE(),"JJJJ-MM-TT") works on German computers. This VBA macro adds =PORTABLE_TODAY(), which fixes this problem and works on any computer.
Public Function PORTABLE_TODAY()
    Dim monthString As String
    Dim dayString As String
    Dim month As Integer
    Dim day As Integer
    
    month = Int(DatePart("m", Now()))
    day = Int(DatePart("d", Now()))
    
    If month < 10 Then
        monthString = "0" & month
    Else
        monthString = month
    End If
    
    If day < 10 Then
        dayString = "0" & day
    Else
        dayString = day
    End If
    
    PORTABLE_TODAY = DatePart("yyyy", Now()) & "-" & monthString & "-" & dayString
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment