[RESOLVED] Forcing custom date passed Regional Settings
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: [RESOLVED] Forcing custom date passed Regional Settings

  1. #1
    Join Date
    Mar 2007
    Posts
    56

    [RESOLVED] Forcing custom date passed Regional Settings

    Hi everyone,

    This has been driving me batty for the last while. I have an application that is passing up data to a SQL server table. Part of the data is dates. Because of settings on the SQL server the update will not accept the MM/dd/yyyy format. I have been trying for quite some time to get around this.

    For some more information. Changing the update method or the server regional settings is not an option and I don't want to change my regional settings as this may not reflect the production server when this goes live.

    Here is what I have tried so far

    Code:
            Dim dateEnd As Date = Date.MinValue
            Dim dateStart As Date = Date.MinValue
    
            If IsDate(Me.txtPostEnd.Text) Then
                dateEnd = CType(Me.txtPostEnd.Text, Date)
                dateEnd = dateEnd.ToString("dd/MM/yyyy")
                dbJobAppData.EndDate = dateEnd.ToShortDateString
           End If
    I hae also tried:

    Code:
            If IsDate(Me.txtPostEnd.Text) Then
                dateEnd = CType(Me.txtPostEnd.Text, Date)
                dbJobAppData.EndDate = CDate(dateEnd.ToString("dd/MM/yyyy"))
            End If
    When run I receive error that conversion of string '28/02/2011' to type Date is not valid.

    It works fine if the day is 12 or below because it still thinks that is the month. Is there a way to force the code to recogize that I want to use dd/MM/yyyy format or even dd/MMM/yyyy format?

    This has to be done at thhe vb code side as the update to the SQL server is completed via a custom framework so I can't customize the SQL code to insert the data using a CONVERT

    Thanks for any suggestions.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Forcing custom date passed Regional Settings

    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!

  3. #3
    Join Date
    Mar 2007
    Posts
    56

    Re: Forcing custom date passed Regional Settings

    Thanks for the reply.

    Won't this affect the entire form (or in my case the entire asp page)? Are dates controlled by the localization; meaning that North American localization will not allow me to use the format I wish to use?

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Forcing custom date passed Regional Settings

    If you detect the current settings, you can modify the query to fit the locale
    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!

  5. #5
    Join Date
    Mar 2007
    Posts
    56

    Re: Forcing custom date passed Regional Settings

    Ok, so I am either doing something completely wrong or misunderstood your post (probably both).

    I have tried using the culture settings in localizing my data as follows:

    Code:
            If IsDate(Me.txtPostEnd.Text) Then
                dateEnd = CType(Me.txtPostEnd.Text, Date).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                MsgBox(dateEnd.ToString("dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")))
                dateEnd = dateEnd.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
                dbJobAppData.EndDate = dateEnd.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture)
    
            End If
    I tested the data using a msgbox and am getting the correct result of 28/02/2011 however when the value is assigned to the variable DateEnd and dbJobAppData.EndDate the value is still resolving to 02/28/2011.

    Am I doing something wrong here. Am I missing something entirely.

    I even tried dateEnd = dateEnd.toshortDate (my system's shortDate is set to dd/MM/yyyy.

    I am really confused why the format is not holding on variable assigment.

    Thanks for your continued help

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Forcing custom date passed Regional Settings

    Download the samples and try out their programs
    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!

  7. #7
    Join Date
    Mar 2007
    Posts
    56

    Re: Forcing custom date passed Regional Settings

    I'm beginning to wonder if there isn't something else going on. I used the samples as you suggested, and the dates were coming in fine. I expected this as all the dates were being passed to a text box and I was able to recreate the correct format using a similar method, however when I put break points on the samples and ran it when at the breakpoing the date variables all had a value of 2/14/2011, but were showing up as 14/02/2011 in the form view.

    I then broke down and changed all of my regional settings on my system to dd/MM/yyyy. Reran the samples code and received the same results. It will display to text the format I want, but when passing values within the code it still passes using the format MM/d/yyyy

    Is there any settings in Visual Studio 2010 that can control this, or is it strictly controlled by the regional settings?

    Do you happen to have any other suggestions?


    Thanks again

  8. #8
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Forcing custom date passed Regional Settings

    The DB engine has regional settings for the DB.
    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!

  9. #9
    Join Date
    Mar 2007
    Posts
    56

    Re: Forcing custom date passed Regional Settings

    Ok, so excuse me for a second while I remove the egg from my face. Turns out the error while correct was for a different field I was passing up. Once corrected all others worked fine and the formatting was not necassary.


    Thanks again for your help.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center