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

你离开我真会死。 提交于 2019-12-13 07:56:33

问题


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 ]


回答1:


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.




回答2:


I don't see a better way than to manually construct the date string in your own function:

Function FormatDate(sourceDate as Variant) As String

    Dim months[1 to 12] as String
    months[1] = "Jan"
    months[2] = "Feb"
    months[3] = "Mar"
    months[4] = "Apr"
    months[5] = "May"
    months[6] = "Jun"
    months[7] = "Jul"
    months[8] = "Aug"
    months[9] = "Sep"
    months[10] = "Oct"
    months[11] = "Nov"
    months[12] = "Dec"

    Dim monthPart as String
    Dim dayPart as String
    Dim yearPart as String

    dayPart = Format(sourceDate, "dd")
    yearPart = Format(sourceDate, "yyyy")
    monthPart = months[Month(sourceDate)]

    FormatDate = dayPart & " " & monthPart & " " & yearPart

End Function



回答3:


I believe that Format(Now, "dd mmm yyyy") will produce the month in English, but I'm not 100% certain.

If not, you could use

Day(Now) & " " & Mid("JanFebMarAprMayJulJunAugSepOctNovDec", 3* Month(Now) -2, 3) & " " & Year(Now)



来源:https://stackoverflow.com/questions/24178211/how-to-specify-months-language-cultureinfo-in-lotusscript-format-now-dd-mm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!