怎样用api修改区域选项中时间格式,短日期yyyy-m-d改成长日期yyyy-mm-dd

解决方案 »

  1.   

    修改日期格式的系统API的说明主要需要使用下面的windows APIBOOL SetLocaleInfo(LCID Locale, // locale identifierLCTYPE LCType, // type of information to setLPCTSTR lpLCData // pointer to information to set); 本函数主要用于windows系统中设置系统的区域选项,主要包括时间,语言等选项的设置。LCID:locale identifier (区域标志)在系统中有两个默认值:LOCALE_SYSTEM_DEFAULT:系统默认的区域选项;LOCALE_USER_DEFAULT:当前用户的区域选项;同时用户也可以通过 MAKELCID宏动态创建LCID;LCType:需要设定的系统信息类型;主要包括下面的值:LOCALE_ICALENDARTYPE LOCALE_SDATELOCALE_ICURRDIGITS LOCALE_SDECIMALLOCALE_ICURRENCY LOCALE_SGROUPINGLOCALE_IDIGITS LOCALE_SLISTLOCALE_IFIRSTDAYOFWEEK LOCALE_SLONGDATELOCALE_IFIRSTWEEKOFYEAR LOCALE_SMONDECIMALSEPLOCALE_ILZERO LOCALE_SMONGROUPINGLOCALE_IMEASURE LOCALE_SMONTHOUSANDSEPLOCALE_INEGCURR LOCALE_SNEGATIVESIGNLOCALE_INEGNUMBER LOCALE_SPOSITIVESIGNLOCALE_ITIME LOCALE_SSHORTDATELOCALE_S1159 LOCALE_STHOUSANDLOCALE_S2359 LOCALE_STIMELOCALE_SCURRENCY LOCALE_STIMEFORMATlpLCData:需要设定的信息的存放地址; 与起对应的函数为int GetLocaleInfo(LCID Locale, // locale identifier LCTYPE LCType, // type of information LPTSTR lpLCData, // address of buffer for information int cchData // size of buffer );
      

  2.   

    SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, "yyyy-MM-dd");
      

  3.   

    '--------------------------------------------------------
    'Purpose: Change system shortdate format
    'Example: Call mdlChangeSystemDateFormat("yyyy/MM/dd")
    '--------------------------------------------------------Public Const LOCALE_SSHORTDATE As Long = &H1F
    Public Const LOCALE_USER_DEFAULT As Long = &H400Public Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal lLocale As Long, ByVal lLocaleType As Long, ByVal sLCData As String, ByVal lBufferLength As Long) As Long
    Public Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As LongPublic Function mdlChangeSystemDateFormat(strFormat As String) As Boolean    Dim strShortDateFormat As String, strBuffer As String
        Dim lBuffSize As Long, lRetVal As Long
        
        lBuffSize = 256
        strBuffer = String(lBuffSize, vbNullChar)
        
        'Get current short date format
        lRetVal = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, strBuffer, lBuffSize)
        
        If lRetVal > 0 Then
            strShortDateFormat = Left(strBuffer, lRetVal - 1)
        Else
            Exit Function
        End If
        
        'If current short date format is different from your format, change it.
        'Note: MMM should be used in capital for month,small m are for minutes
        If Ucase(strShortDateFormat) <> Ucase(strFormat) Then
            lRetVal = SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, strFormat)
        End If
        
        If lRetVal > 0 Then mdlChangeSystemDateFormat = True
    End Function