Results 1 to 17 of 17

Thread: Date Unwanted Convert !

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Date Unwanted Convert !

    Hi
    First of all I should Thanks for your time .
    When I change my Windows region To Persian , When i Want to add Date Value to my data table with Date Type , The Program See my Value As Hejri Calendar and convert it to gregorian calendar .
    if i want to explain my problem :
    Code:
    Dim DataTable as new DataTable
     DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
            DataTable.Rows.Add("2019-08-27")
    And See This Value in my table :
    #11/18/2640 #
    i don't know which section Change my Date Value . I be thankful if you solve my problem.

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Date Unwanted Convert !

    And See This Value in my table
    How are you seeing this value?

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    with debug mode and stop program and see value in watch

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    See This code please :
    Code:
      
     DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
            DataTable.Rows.Add("2019-08-27")
            Dim DR() As DataRow
            DR = DataTable.Select("DT = #" + "2019-08-27" + "#")
    the Dr.count = 0 ! ! !

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Date Unwanted Convert !

    Welcome to VBForums

    The Watch window (and the tools for most databases, as well as most other software) displays values using your regional settings, and you generally cannot do anything about that. In the Watch window you can alter the display by changing the value to use ToString, eg: MyDateValue.ToString("yyyy-MM-dd")

    The important thing is that the value is correct, because that is all that gets saved.

    The format is purely a display issue, so as long as your program displays/reads values correctly, everything is fine.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    Thank you for reply ,but i think the value is wrong in data table because i add above code (post 4 and post 5) and see wrong result.

  7. #7
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Date Unwanted Convert !

    I was just going to say that you cant change the format of the date in the datatable because there is not such thing. This is a display instance derived from region.

    In your statement you may instead want to try parameters using culture settings.

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    can you explain the culture setting , what's your idea .thank's

  9. #9
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Date Unwanted Convert !

    This is really uncharted waters for me, but try something like this (untested)
    Code:
            Dim MyDate As DateTime = Nothing
            Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
    
            Using Cmd As New SqlCommand("Select * from tbl where datecol=@Mydate")
                Cmd.Parameters.AddWithValue("@Mydate", MyDate)
            End Using
    This could be way off, or there could be a totally easier way... /shrug

  10. #10
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Date Unwanted Convert !

    Ops, you wanted to return datarows

    My eyes are getting bad of late. I think I need vac.

    So without the command/params

    Code:
     Dim MyDate As DateTime = Nothing
            Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
    
    DR = DataTable.Select("DT = #" & MyDate  & "#")

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    thank you very much , i use your method when i want to Write data and for search , it work very well for me .thanks

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    After your hint ,my test code convert to this
    Code:
      DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
            Dim MyDate As DateTime = Nothing
            Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
            DataTable.Rows.Add(MyDate)
            Dim DR() As DataRow
            Dim Sr As String = MyDate.ToShortDateString
            DR = DataTable.Select("DT = #" & Sr & "#")
      'SR = "05/06/1398"
    But as you see problem convert to something else , this time the program see Date as gregorian and convert it to persian ! ! !
    I realy don't know what happening. . .

  13. #13

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    After your hint ,my test code convert to this
    Code:
      DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
            Dim MyDate As DateTime = Nothing
            Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
            DataTable.Rows.Add(MyDate)
            Dim DR() As DataRow
            Dim Sr As String = MyDate.ToShortDateString
            DR = DataTable.Select("DT = #" & Sr & "#")
      'SR = "05/06/1398"
    But as you see problem convert to something else , this time the program see Date as gregorian and convert it to persian ! ! !
    I realy don't know what happening. . .

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Date Unwanted Convert !

    Quote Originally Posted by Hamid.Khp View Post
    Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
    That line tries to store the value interpreted using US format (so that value will be in a few months time).

    However, it is important to note that it tries to convert it. If for any reason it fails, there will not be an error - it will simply return False (and the variable value wont be set), but that currently isn't being checked.

    This is how you could check it:
    Code:
    If Date.TryParseExact("2019-08-27", ..., MyDate) Then
      'your other code
    Else
      'show a message to say it didn't convert
    End If
    Quote Originally Posted by Hamid.Khp View Post
    Dim Sr As String = MyDate.ToShortDateString
    That creates a string value which is formatted according to Regional Settings... which will be a different format to the one you started with.

  15. #15

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    NO It Return True(After Check it) ,I really don't know what happening , first It don't Convert my date but after that my date convert automatically ,perhaps i should change my question like this :
    What can i do if I want everybody with any region of windows don't see error in my program ?
    thank's again.

  16. #16

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    NO It Return True(After Check it) ,I really don't know what happening , first It don't Convert my date but after that my date convert automatically ,perhaps i should change my question like this :
    What can i do if I want everybody with any region of windows don't see error in my program ?
    thank's again.

  17. #17

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    11

    Re: Date Unwanted Convert !

    finaly it work
    thanks everyone ,you give me idea to solve it ,for another persones :
    Code:
            DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
            Dim MyDate As DateTime = Nothing
            Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
            DataTable.Rows.Add(MyDate)
            Dim DR() As DataRow
            Dim Sr As String = MyDate.ToString("yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"))
            DR = DataTable.Select("DT = #" & Sr & "#")
    Last edited by Hamid.Khp; Mar 22nd, 2019 at 08:10 AM.

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