Search code examples
lotus-noteslotusscriptdate-formattingregional-settings

how to specify month's language (CultureInfo) in Lotusscript Format (now, "dd/mmm/yyyy")


This question is the LOTUSCRIPT version of datetime.tostring month and day language

Need description: I need a string date in the dd/mmm/yyyy format (ex: "28 feb 2014"). I don't want english (intl) language for this 3 letters, and not the default regional setting in use in the LOCAL client.

Constraints:

  1. Programmatic language used: Lotusscript on client side in Lotus Notes.
  2. I can't change the regional setting of the client computer. Barely acceptable a specific to Lotus Notes registry is painful (like: http://searchdomino.techtarget.com/tip/Resolve-Domino-date-format-problems-for-dd-mm-yyyy-format)

I guess format$ will not solve my problem. What can I use ? My last resort will be select case month(now) case 1: resu = resu + " jan " ....

Any better idea ? Thanks in advance for a such "deja vu" topic.

[Edited I wrote previously "I wan't English" when it should be "I don't want". Format in LS ALWAYS returns english ]


Solution

  • If you are using Windows you can use WinApi GetDateFormat function. For this function you need to create SYSTEMTIME structure. Also you need to use Language Identifier Constants and Strings and Day, Month, Year, and Era Format Pictures topics for setting language and format of date and time.
    Here is example:

    'Declarations
    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
    
    Declare Function GetDateFormat Lib "kernel32" Alias "GetDateFormatA" (_
    Byval Locale As Long,_
    Byval dwFlags As Long,_
    lpDate As SYSTEMTIME,_
    Byval lpFormat As String,_
    Byval lpDateStr As String,_
    Byval cchDate As Long) As Long
    
    Function FormatDate(value As Variant, locale As Long, formatString As String) As String
    
        Dim buffer As String, systemTime As SYSTEMTIME
    
        systemTime.wYear = Year(value)
        systemTime.wMonth = Month(value)
        systemTime.wDay = Day(value)
    
        buffer = String(255, 0)
    
        GetDateFormat locale&, 0, systemTime, formatString$ , buffer, Len(buffer)
    
        FormatDate$ = Left$(buffer, Instr(1, buffer, Chr$(0)) - 1)
    
    End Function
    
    'Usage
    MessageBox FormatDate(Now, &h40c, "dd MMM yyyy")
    '&h40c - is fr-FR locale (0x040c)
    


    Another way is to use LS2J. For this you can use SimpleDateFormat class and its format method. Also you need to use Locale class and Calendar class for setting language and date.
    Here is example:

    'Declarations
    Uselsx "*javacon"'Include this for using Java objects in LotusScript
    
    Function FormatDate(value As Variant, language As String, country As String, formatString As String) As String
    
        Dim javaSession As New JavaSession
    
        Dim localeClass As JavaClass
        Dim locale As JavaObject
    
        Dim calendarClass As JavaClass
        Dim calendar As JavaObject
    
        Dim dateFormatClass As JavaClass
        Dim dateFormat As JavaObject
    
        Set localeClass = javaSession.GetClass("java/util/Locale")
        Set locale = localeClass.CreateObject("(Ljava/lang/String;Ljava/lang/String;)V", language$, country$)
    
        Set calendarClass = javaSession.GetClass("java/util/Calendar")
        Set calendar = calendarClass.GetMethod("getInstance", "()Ljava/util/Calendar;").Invoke()
        'You need to subtract 1 from month value
        Call calendar.set(Year(value), Month(value) - 1, Day(value))
    
        Set dateFormatClass = javaSession.GetClass("java/text/SimpleDateFormat")
        Set dateFormat = dateFormatClass.CreateObject("(Ljava/lang/String;Ljava/util/Locale;)V", formatString$, locale)
    
        FormatDate$ = dateFormat.format(calendar.getTime())
    
    End Function
    
    'Usage
    MessageBox FormatDate(Now, "fr", "FR", "dd MMM yyyy")
    

    In this example I have used this constructor for getting Locale object. You can get language codes from here and country codes from here.
    For SimpleDateFormat object I have used this constructor.