首页 /编程语言和算法/VB6/ASP
 VB6 改变系统的日期格式
昨天 19:41

新建From1(窗体),新建Command1(按钮CommandButton),代码:

Private Const LOCALE_SSHORTDATE As Long = &H1F
Private Const LOCALE_USER_DEFAULT As Long = &H400
Private 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
Private Declare Function SetLocaleInfo Lib "kernel32" _
        Alias "SetLocaleInfoA" (ByVal Locale As Long, _
        ByVal LCType As Long, ByVal lpLCData As String) As Long

Private Sub Command1_Click()
    Dim shortDateFormat As String
    Dim lBuffSize As String
    Dim sBuffer As String
    Dim lRetGet As Long
    Dim lRetSet As Long
    lBuffSize = 256
    sBuffer = String$(lBuffSize, vbNullChar)
    'get the date information in buffer
    lRetGet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sBuffer, lBuffSize)
    If lRetGet > 0 Then
        shortDateFormat = Left$(sBuffer, lRetGet - 1)
        'this is the existing format of machine
    End If
    'to change the format if doesn't matches ur format
    'MM should be used in capital for monyhs,small m are for minutes
    If UCase(shortDateFormat) <> "DD/MM/YYYY" Then
        lRetSet = SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, "yyyy/MM/dd") '这里只能MM大写,其它小写否则要出错
        'on sucess lretset have value greater than 0
        If lRetSet <= 0 Then
            MsgBox "Date format not changed"
            Exit Sub
        End If
    End If
    MsgBox "Okay"
End Sub

运行得到Okay后,可以点击Windows的时间设置,同步一下时间就看到更新格式了。

 
全部回复(0)
首页 | 电脑版 |