CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Changing Date time Format

    Take a look for ScriptoMatic2 which is a free app by the MS Guys. It writes scripts to do Windows Level things. You may be able to alter it that way, or possibly in the registry. I think you'd have to re-start the machine for them to take effect, though.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  2. #17
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Changing Date time Format

    Amarjit : Based on your PM.

    This was found on http://www.a1vbcode.com/app-2790.asp.

    You're gonna need the following APIs :
    GetSystemDefaultLCID
    GetLocaleInfo
    SetLocaleInfo
    EnumDateFormats
    PostMessage
    CopyMemory

    So, let's put all these (alongwith the associated Constants - and the functions using these APIs) in to a module :
    Code:
    Option Explicit
    
    Public Const LOCALE_SLANGUAGE As Long = &H2
    Public Const LOCALE_SSHORTDATE As Long = &H1F
    
    Public Const DATE_LONGDATE As Long = &H2
    Public Const DATE_SHORTDATE As Long = &H1
    
    Public Const HWND_BROADCAST As Long = &HFFFF&
    Public Const WM_SETTINGCHANGE As Long = &H1A
    
    Public Declare Function PostMessage Lib "user32" _
      Alias "PostMessageA" _
     (ByVal hwnd As Long, _
      ByVal wMsg As Long, _
      ByVal wParam As Long, _
      lParam As Any) As Long
    
    Public Declare Function EnumDateFormats Lib "kernel32" _
      Alias "EnumDateFormatsA" _
     (ByVal lpDateFmtEnumProc As Long, _
      ByVal Locale As Long, _
      ByVal dwFlags As Long) As Long
    
    Public Declare Sub CopyMemory Lib "kernel32" _
      Alias "RtlMoveMemory" _
     (Destination As Any, _
      Source As Any, _
      ByVal Length As Long)
    
    Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
    
    Public Declare Function GetLocaleInfo Lib "kernel32" _
      Alias "GetLocaleInfoA" _
     (ByVal Locale As Long, _
      ByVal LCType As Long, _
      ByVal lpLCData As String, _
      ByVal cchData 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 Long
    
    
    Public Function fGetUserLocaleInfo(ByVal lLocaleID As Long, _
               ByVal lLCType As Long) As String
    
    Dim sReturn As String
    Dim lReturn As Long
    lReturn = GetLocaleInfo(lLocaleID, lLCType, sReturn, Len(sReturn))
    If lReturn Then
    
       sReturn = Space$(lReturn)
       If lReturn Then
           fGetUserLocaleInfo = Left$(sReturn, lReturn - 1)
       End If
    End If
       
    End Function
    
    
    Public Function theEnumDates(lDateFormatString As Long) As Long
    theEnumDates = 1
    End Function
    
    
    Private Function GetStrFromPointer(sString As Long) As String
    Dim lPos    As Long
    Dim sBuffer As String
    sBuffer = Space$(128)
    Call CopyMemory(ByVal sBuffer, sString, ByVal Len(sBuffer))
    lPos = InStr(sBuffer, Chr$(0))
    
    If lPos Then
       GetStrFromPointer = Left$(sBuffer, lPos - 1)
    End If
    End Function
    Now, add a button to your form and type this :
    Code:
    Private Sub Command1_Click()
       Dim xCID As Long
       Dim xChangedFormat As String
       '
       xCID = GetSystemDefaultLCID()
       xChangedFormat = "MM/dd/yyyy" 'specify date format here!!!
       '
       If xChangedFormat <> "" Then
           Call SetLocaleInfo(xCID, LOCALE_SSHORTDATE, xChangedFormat)
           Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)
           Call EnumDateFormats(AddressOf theEnumDates, xCID, DATE_SHORTDATE)
           MsgBox "Date Changed to MM/dd/yyyy format"
       End If
    End Sub
    Try this, I honestly haven't tested this thoroughly, but it should work

    Another much simpler option may be to run the regional Settings applet from within your program. basically, it will open the Regional settings dialog box, and let the user change the date formats and stuff. Add this to a button click :

    Code:
    Private Sub Command2_Click()
    Call Shell("rundll32.exe shell32.dll,Control_RunDLL intl.cpl")
    
    End Sub
    I hope this helps a bit!
    Last edited by HanneSThEGreaT; October 20th, 2007 at 01:58 AM.

  3. #18
    Join Date
    Feb 2005
    Location
    Kolkata, India
    Posts
    430

    Re: Changing Date time Format

    Thanks Hannes for that answer. I will reply you after testing it.

    Meanwhile another answer bugs me regarding using dates in the access query.

    Most of the members have told me not to change the user settigs of the date. As the same may effect them somehow. Can you give me any possible solution regarding using of date ranges in SQL search.

    Is it possible to know without the use of any API what is the present date format (long & Short) of the system.

    Actually i am bumping into the problem when i fire a query into access 2000 without giving any attention to the date format of the system. If the system date is set to mm/dd/yyyy then the date such as 12/31/2006 is perfectly interpreted whereas 05/06/2006 (5th June 2006) as 5th May 2006. Hence the results returned is not correct.

    I know many may have bumped into this problem. I think there may be a possible solution regarding using date formats without depending on the system date format set in the regional settings.

    Any further help....

    Thanks always

    regards
    Amarjit
    Try, Try Hard, Try Harder one day you will Succeed.

  4. #19
    Join Date
    Oct 2006
    Location
    slavia
    Posts
    42

    Re: Changing Date time Format

    hi,
    just wanna try to help, i dont know whether this can give you any solution
    what if you try to use the DatePart Function, so that whatever the format of the date, the datepart always give u the right answer.
    ex :
    MM/DD/YYYY => 10/12/2006
    DD/MM/YYYY=> 12/10/2006

    Code:
    dim d,m, y as string
    dim dates 
    
    dates = "12/10/2006"
    d = datepart("d",dates)
    m = datepart("m", dates)
    y = datepart ("y", dates)
    hope that u got what i mean.

    rgrds,

    Ecko

  5. #20
    Join Date
    Feb 2005
    Location
    Kolkata, India
    Posts
    430

    Re: Changing Date time Format

    Thank for the help.

    But my question is how will i know the present format of the date whether it is mm/dd/yyyy or dd/mm/yy or yyyy/dd/mm etc ......

    Is there any way too know that

    Regards
    Amarjit
    Try, Try Hard, Try Harder one day you will Succeed.

  6. #21
    Join Date
    Sep 2006
    Posts
    635

    Re: Changing Date time Format

    I hoe it's useful

    Code:
    Private Const LOCALE_SSHORTDATE As Long = &H1F
    Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" ( _
    		ByVal Locale As Long, _
    		ByVal LCType As Long, _
    		ByVal lpLCData As String, _
    		ByVal cchData As Long) As Long
    Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
    Private Function GetDateFormat() As String
       Dim Val_x As Long
       Dim Simbolo As String
       
       Dim r1 As Long
       Dim r2 As Long
       Dim p As Integer
       Dim Locale As Long
       
       Val_x = LOCALE_SSHORTDATE
       Locale = GetUserDefaultLCID()
       r1 = GetLocaleInfo(Locale, Val_x, vbNullString, 0)
       
       Simbolo = String$(r1, 0)
       
       r2 = GetLocaleInfo(Locale, Val_x, Simbolo, r1)
       
       p = InStr(Simbolo, Chr$(0))
       If p > 0 Then
    	  GetDateFormat = Left$(Simbolo, p - 1)
       End If
    End Function
    test
    Code:
     
    Private Sub Command_Click()
    Dim Sd() As String
    Dim DateFormat As String
    DateFormat = GetDateFormat
    Sd = Split(DateFormat, "/")
    
    Debug.Print DateFormat
    Debug.Print Sd(0)
    Debug.Print Sd(1)
    Debug.Print Sd(2)
    End Sub

  7. #22
    Join Date
    Jun 2005
    Posts
    12

    Re: Changing Date time Format

    Hanesa,

    I got the VB6 code from this link, its the same as you have posted
    http://www.a1vbcode.com/app-2790.asp

    and here with i attached the project which is converted from VB6 to VB.Net2005. Kindly check this. Totally it shows 6 errors when i Build the project.
    the following error repeats.

    Error 1 'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type.

    Error 4 'As Any' is not supported in 'Declare' statements.

    i changed the Any to Object its ok fine. But the AddressOf is not working. I don't know how to change it as Delegate. Can you pls modify and send me the attached project.

    Thanks for your support.

    Thanks and Regards,
    V.Chock.
    Attached Files Attached Files

  8. #23
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Changing Date time Format

    Quote Originally Posted by itchock
    Hanesa,

    I got the VB6 code from this link, its the same as you have posted
    http://www.a1vbcode.com/app-2790.asp
    Are you sure ¿ Then I should have mentioned it there ( I mentioned it there now ). I did something similar in any case, so there must have been a confusion somewhere. Sorry about that, it's difficult if you have hundreds of stuff.

    Quote Originally Posted by itchock
    and here with i attached the project which is converted from VB6 to VB.Net2005. Kindly check this. Totally it shows 6 errors when i Build the project.
    Don't get me wrong, but for the 100th time people, it is not good to use the "upgrade wizard"! Mainly because of these type of errors. IMHO, it is best to redo the entire applicaton, or even try an incremental update.
    Any is not supported in .NET. AddressOf works a bit differently in .NET

    The proper definitions for those APIs ( for .NET ) is the following :
    Code:
    Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    Private Declare Function EnumDateFormats Lib "KERNEL32.dll" Alias "EnumDateFormatsA" (ByVal lpDateFmtEnumProc As Int32, ByVal Locale As Int32, ByVal dwFlags As Int32) As Int32
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Int32, ByRef Source As Any, ByVal Length As Int32)
    Private Declare Function GetSystemDefaultLCID Lib "kernel32.dll" () As Int32
    Private Declare Function SetLocaleInfo Lib "kernel32.dll" Alias "SetLocaleInfoA" (ByVal Locale As Int32, ByVal LCType As Int32, ByVal lpLCData As String) As Int32
    Private Declare Function GetLocaleInfo Lib "kernel32.dll" Alias "GetLocaleInfoA" (ByVal Locale As Int32, ByVal LCType As Int32, ByVal lpLCData As String, ByVal cchData As Int32) As Int32
    Have a look at this article explaining the differences between API in VB 6 and .NET :
    http://www.codeguru.com/vb/gen/vb_ge...le.php/c11981/

    make sure you download the API Viewer 2004 ( for the proper .NET API declarations ) :
    http://www.activevb.de/rubriken/apiv...viewereng.html

    A better way ( the actual way to use unmanaged code in .NET ) is to use the System.Runtime.InterOpServices namespace :
    http://msdn2.microsoft.com/en-us/lib...pservices.aspx

    Here's more info on delegates in .NET :
    http://msdn2.microsoft.com/en-us/lib...22(VS.80).aspx

    And here's more info on localisation in .NET :
    http://www.c-sharpcorner.com/UploadF...et_CSharp.aspx

    Remember, this is the VB 6 forum. If you have VB.NET questions, visit the VB.NET forum :
    http://www.codeguru.com/forum/forumdisplay.php?f=12

    Now, please, let me get the rest my mind deserves ( I don't have "Taking A Break" in my signature for nothing. )

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured