gmaxey
09-22-2014, 05:58 AM
Graham, Paul,
Thanks for your replies. I am aware of the InsertDateTime method and should have been clearer on my requirement. I am trying to define the label on a series of ribbon menu buttons, e.g., returnedVal = fcnGetFrench(Format(Now(), "d MMMM yyyy"))
Function fcnGetFrench(strIn As String) As String
Dim strFrench As String
strFrench = strIn
strFrench = Replace(strFrench, "Monday", "lundi")
strFrench = Replace(strFrench, "Tuesday", "mardi")
strFrench = Replace(strFrench, "Wednesday", "mercredi")
strFrench = Replace(strFrench, "Thursday", "jeudi")
strFrench = Replace(strFrench, "Friday", "vendredi")
strFrench = Replace(strFrench, "Saturday", "samedi")
strFrench = Replace(strFrench, "Sunday", "dimanche")
strFrench = Replace(strFrench, "January", "janvier")
strFrench = Replace(strFrench, "February", "février")
strFrench = Replace(strFrench, "March", "mars")
strFrench = Replace(strFrench, "April", "avril")
strFrench = Replace(strFrench, "May", "mai")
strFrench = Replace(strFrench, "June", "juin")
strFrench = Replace(strFrench, "July", "juillet")
strFrench = Replace(strFrench, "August", "août")
strFrench = Replace(strFrench, "September", "septembre")
strFrench = Replace(strFrench, "October", "octobre")
strFrench = Replace(strFrench, "November", "novembre")
strFrench = Replace(strFrench, "December", "décembre")
fcnGetFrench = strFrench
End Function
It works but was looking for a more direct way. Thanks!
gmaxey
09-23-2014, 08:34 PM
Paul,
While I don't think it can ever be perfect (just too many variables e.g, the first day of the month in a French date is 1st with the st superscripted) but this provides a pretty good result:
Option Explicit
Private Declare Function GetLocaleInfoEx Lib "kernel32" (ByVal lpLocaleName As Long, _
ByVal LCType As Long, ByVal lpLCData As Long, ByVal cchData As Long) As Long
Public Const LOCALE_SDAYNAME1 As Long = &H2A 'long name for Monday
Public Const LOCALE_SDAYNAME2 As Long = &H2B 'long name for Tuesday
Public Const LOCALE_SDAYNAME3 As Long = &H2C 'long name for Wednesday
Public Const LOCALE_SDAYNAME4 As Long = &H2D 'long name for Thursday
Public Const LOCALE_SDAYNAME5 As Long = &H2E 'long name for Friday
Public Const LOCALE_SDAYNAME6 As Long = &H2F 'long name for Saturday
Public Const LOCALE_SDAYNAME7 As Long = &H30 'long name for Sunday
Public Const LOCALE_SABBREVDAYNAME1 As Long = &H31 'short name for Monday
Public Const LOCALE_SABBREVDAYNAME2 As Long = &H32 'short name for Tuesday
Public Const LOCALE_SABBREVDAYNAME3 As Long = &H33 'short name for Wednesday
Public Const LOCALE_SABBREVDAYNAME4 As Long = &H34 'short name for Thursday
Public Const LOCALE_SABBREVDAYNAME5 As Long = &H35 'short name for Friday
Public Const LOCALE_SABBREVDAYNAME6 As Long = &H36 'short name for Saturday
Public Const LOCALE_SABBREVDAYNAME7 As Long = &H37 'short name for Sunday
Public Const LOCALE_SMONTHNAME1 As Long = &H38
Public Const LOCALE_SMONTHNAME2 As Long = &H39
Public Const LOCALE_SMONTHNAME3 As Long = &H3A
Public Const LOCALE_SMONTHNAME4 As Long = &H3B
Public Const LOCALE_SMONTHNAME5 As Long = &H3C
Public Const LOCALE_SMONTHNAME6 As Long = &H3D
Public Const LOCALE_SMONTHNAME7 As Long = &H3E
Public Const LOCALE_SMONTHNAME8 As Long = &H3F
Public Const LOCALE_SMONTHNAME9 As Long = &H40
Public Const LOCALE_SMONTHNAME10 As Long = &H41
Public Const LOCALE_SMONTHNAME11 As Long = &H42
Public Const LOCALE_SMONTHNAME12 As Long = &H43
Public Const LOCALE_SABBREVMONTHNAME1 As Long = &H44
Public Const LOCALE_SABBREVMONTHNAME2 As Long = &H45
Public Const LOCALE_SABBREVMONTHNAME3 As Long = &H46
Public Const LOCALE_SABBREVMONTHNAME4 As Long = &H47
Public Const LOCALE_SABBREVMONTHNAME5 As Long = &H48
Public Const LOCALE_SABBREVMONTHNAME6 As Long = &H49
Public Const LOCALE_SABBREVMONTHNAME7 As Long = &H4A
Public Const LOCALE_SABBREVMONTHNAME8 As Long = &H4B
Public Const LOCALE_SABBREVMONTHNAME9 As Long = &H4C
Public Const LOCALE_SABBREVMONTHNAME10 As Long = &H4D
Public Const LOCALE_SABBREVMONTHNAME11 As Long = &H4E
Public Const LOCALE_SABBREVMONTHNAME12 As Long = &H4F
Public Const LOCALE_SNAME As Long = &H5C
Function GetInfo(ByVal lInfo As Long, Optional LocaleName As String = "en-US") As String
'http://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx
Dim sLocaleName As String
Dim sRetBuffer As String
Dim nCharsRet As Long
sLocaleName = LocaleName & Chr$(0)
sRetBuffer = Space(256)
nCharsRet = GetLocaleInfoEx(StrPtr(sLocaleName), lInfo, StrPtr(sRetBuffer), Len(sRetBuffer) - 1)
GetInfo = Left$(sRetBuffer, nCharsRet)
GetInfo = Left(GetInfo, Len(GetInfo) - 1) 'Added GKM - printed result was displaying and odd AscW(0) character
lbl_Exit:
Exit Function
End Function
Sub DemoInsertDate()
'http://www.science.co.il/language/locale-codes.asp
Dim oInputDate As Date
oInputDate = Now - 22
Debug.Print fcnCreateInternationalDate(oInputDate, "DDDD, MMMM d, yyyy", "en-US")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDDD, d MMMM, yyyy", "fr-CA")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDDD, d MMMM, yyyy", "es-PA")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDDD, d MMMM, yyyy", "de-DE")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDD, MMMM d, yyyy", "en-US")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDD, d MMMM, yyyy", "fr-CA")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDD, d MMMM, yyyy", "es-PA")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDD, d MMMM, yyyy", "de-DE")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDD, MMM dd, yyyy", "en-US")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDD, d MMM, yyyy", "fr-CA")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDD, d MMM, yyyy", "es-PA")
Debug.Print fcnCreateInternationalDate(oInputDate, "DDD, d MMM, yyyy", "de-DE")
lbl_Exit:
Exit Sub
End Sub
Function fcnCreateInternationalDate(oDate As Date, strFormat As String, strLCID As String) As String
Dim strDate As String
Dim varWeekday
Dim varMonth
strDate = Format(oDate, strFormat)
If Left(UCase(strFormat), 5) = "DDDD," Then
varWeekday = Choose(Weekday(oDate), GetInfo(LOCALE_SDAYNAME7, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SDAYNAME1, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SDAYNAME2, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SDAYNAME3, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SDAYNAME4, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SDAYNAME5, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SDAYNAME6, _
GetInfo(LOCALE_SNAME)))
Select Case Weekday(oDate)
Case 1: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SDAYNAME7, strLCID))
Case 2: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SDAYNAME1, strLCID))
Case 3: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SDAYNAME2, strLCID))
Case 4: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SDAYNAME3, strLCID))
Case 5: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SDAYNAME4, strLCID))
Case 6: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SDAYNAME5, strLCID))
Case 7: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SDAYNAME6, strLCID))
End Select
ElseIf Left(UCase(strFormat), 4) = "DDD," Then
varWeekday = Choose(Weekday(oDate), GetInfo(LOCALE_SABBREVDAYNAME7, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SABBREVDAYNAME1, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SABBREVDAYNAME2, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SABBREVDAYNAME3, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SABBREVDAYNAME4, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SABBREVDAYNAME5, _
GetInfo(LOCALE_SNAME)), GetInfo(LOCALE_SABBREVDAYNAME6, _
GetInfo(LOCALE_SNAME)))
Select Case Weekday(oDate)
Case 1: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SABBREVDAYNAME7, strLCID))
Case 2: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SABBREVDAYNAME1, strLCID))
Case 3: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SABBREVDAYNAME2, strLCID))
Case 4: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SABBREVDAYNAME3, strLCID))
Case 5: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SABBREVDAYNAME4, strLCID))
Case 6: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SABBREVDAYNAME5, strLCID))
Case 7: strDate = Replace(strDate, varWeekday, GetInfo(LOCALE_SABBREVDAYNAME6, strLCID))
End Select
End If
If InStr(UCase(strFormat), "MMMM") > 0 Then
varMonth = Choose(Month(oDate), GetInfo(LOCALE_SMONTHNAME1, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME2, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME3, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME4, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME5, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME6, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME7, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME8, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME9, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME10, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME11, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SMONTHNAME12, GetInfo(LOCALE_SNAME)))
Select Case Month(oDate)
Case 1: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME1, strLCID))
Case 2: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME2, strLCID))
Case 3: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME3, strLCID))
Case 4: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME4, strLCID))
Case 5: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME5, strLCID))
Case 6: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME6, strLCID))
Case 7: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME7, strLCID))
Case 8: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME8, strLCID))
Case 9: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME9, strLCID))
Case 10: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME10, strLCID))
Case 11: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME11, strLCID))
Case 12: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SMONTHNAME12, strLCID))
End Select
ElseIf InStr(UCase(strFormat), "MMMM") > 0 Then
varMonth = Choose(Month(oDate), GetInfo(LOCALE_SABBREVMONTHNAME1, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME2, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME3, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME4, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME5, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME6, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME7, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME8, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME9, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME10, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME11, GetInfo(LOCALE_SNAME)), _
GetInfo(LOCALE_SABBREVMONTHNAME12, GetInfo(LOCALE_SNAME)))
Select Case Month(oDate)
Case 1: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME1, strLCID))
Case 2: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME2, strLCID))
Case 3: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME3, strLCID))
Case 4: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME4, strLCID))
Case 5: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME5, strLCID))
Case 6: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME6, strLCID))
Case 7: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME7, strLCID))
Case 8: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME8, strLCID))
Case 9: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME9, strLCID))
Case 10: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME10, strLCID))
Case 11: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME11, strLCID))
Case 12: strDate = Replace(strDate, varMonth, GetInfo(LOCALE_SABBREVMONTHNAME12, strLCID))
End Select
End If
'ActiveDocument.Range.InsertAfter strDate & vbCr
fcnCreateInternationalDate = strDate
lbl_Exit:
Exit Function
End Function
Paul_Hossler
09-24-2014, 02:25 PM
You could read the date formats directly and do the replacing
This is a little different in that it only uses the system long and short date formats, but might give you some ideas
Function GetInfo(ByVal lInfo As Long, Optional LocaleName As String = "en-US") As String
'http://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx
Dim sLocaleName As String
Dim sRetBuffer As String
Dim nCharsRet As Long
sLocaleName = LocaleName & Chr$(0)
sRetBuffer = Space(256)
nCharsRet = GetLocaleInfoEx(StrPtr(sLocaleName), lInfo, StrPtr(sRetBuffer), Len(sRetBuffer) - 1)
GetInfo = Left$(sRetBuffer, nCharsRet)
GetInfo = Left(GetInfo, Len(GetInfo) - 1) 'Added GKM - printed result was displaying and odd AscW(0) character
lbl_Exit:
Exit Function
End Function
Sub DemoInsertDate()
'http://www.science.co.il/language/locale-codes.asp
Dim oInputDate As Date
oInputDate = Now - 22
Debug.Print fcnCreateInternationalDate(oInputDate, False, "en-US")
Debug.Print fcnCreateInternationalDate(oInputDate, True, "en-US")
Debug.Print fcnCreateInternationalDate(oInputDate, False, "fr-CA")
Debug.Print fcnCreateInternationalDate(oInputDate, True, "fr-CA")
Debug.Print fcnCreateInternationalDate(oInputDate, False, "de-DE")
Debug.Print fcnCreateInternationalDate(oInputDate, True, "de-DE")
End Sub
Function fcnCreateInternationalDate(oDate As Date, Optional bShortFormat As Boolean = True, Optional strLCID As String = "en-US") As String
Dim strDate As String
Dim iDOW As Long, iMOY As Long, iDay As Long, iYear As Long
Dim sDOW As String, sMOY As String
iDOW = Weekday(oDate)
iDay = Day(oDate)
iMOY = Month(oDate)
iYear = Year(oDate)
If bShortFormat Then
strDate = GetInfo(LOCALE_SSHORTDATE, strLCID)
sDOW = GetInfo(LOCALE_SABBREVDAYNAME1 + iDOW - 1, strLCID)
sMOY = GetInfo(LOCALE_SABBREVMONTHNAME1 + iMOY - 1, strLCID)
Else
strDate = GetInfo(LOCALE_SLONGDATE, strLCID)
sDOW = GetInfo(LOCALE_SDAYNAME1 + iDOW - 1, strLCID)
sMOY = GetInfo(LOCALE_SMONTHNAME1 + iMOY - 1, strLCID)
End If
strDate = UCase(strDate)
If InStr(strDate, "DDDD") > 0 Then
strDate = Replace(strDate, "DDDD", sDOW)
ElseIf InStr(strDate, "DDD") > 0 Then
strDate = Replace(strDate, "DDD", sDOW)
End If
If InStr(strDate, "MMMM") > 0 Then
strDate = Replace(strDate, "MMMM", sMOY)
ElseIf InStr(strDate, "MMM") > 0 Then
strDate = Replace(strDate, "MMM", sMOY)
ElseIf InStr(strDate, "MM") > 0 Then
strDate = Replace(strDate, "MM", Format(iMOY, "0#"))
ElseIf InStr(strDate, "M") > 0 Then
strDate = Replace(strDate, "M", Format(iMOY, "##"))
End If
If InStr(strDate, "DD") > 0 Then
strDate = Replace(strDate, "DD", Format(iDay, "0#"))
ElseIf InStr(strDate, "D") > 0 Then
strDate = Replace(strDate, "D", Format(iDay, "##"))
End If
If InStr(strDate, "YYYY") > 0 Then
strDate = Replace(strDate, "YYYY", Format(iYear, "####"))
ElseIf InStr(strDate, "YY") > 0 Then
strDate = Replace(strDate, "YY", Right(Format(iYear, "####"), 2))
End If
fcnCreateInternationalDate = strDate
End Function
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.