none
How Do I Set or Detect The Date Format (DD/MM/YYYY or MM/DD/YYYY) RRS feed

  • Question

  • Hello Community,

    I am having an issue when running my code on other systems with a different date format to the system I coded the app on.
    Being Australian I have my date format preference set to DD/MM/YYYY and have structured the code to suit this.
    Though running the same code on a system with a date format set to American MM/DD/YYYY causes obvious errors when the day and month no longer match up.

    Please, how do I determine the date format each system uses, or better still set my own date format as my app starts.

    Have looked at DateTimePickerFormat and other examples of similar issues on forums, but I cant get it, or them working.

    Any help would be greatly appreciated, an example would be great too if at all possible please.

    Thank you,

    Rob

    Thursday, May 18, 2017 12:03 AM

Answers

  • thankyou for your reply Charles,

    Actually I may have over thought the issue, Just been playing with it then and it seems if I just run the Date through a function such as

      DateToConvert = DateAndTime.Day(DateToConvert) & "/" & DateAndTime.Month(DateToConvert) & "/" & DateAndTime.Year(DateToConvert)

    seems to work all too well, and I can restructure the format any way I want very easily.

    Would still be good to know how to get the date format preference set by windows though during runtime, if at all possible?

    Am very sorry to waist your time. I think this will work OK for me for now.
    Appreciate your reply though all the same.



    • Edited by AussieHack Thursday, May 18, 2017 12:41 AM
    • Marked as answer by AussieHack Thursday, May 18, 2017 12:50 AM
    Thursday, May 18, 2017 12:41 AM
  • Though running the same code on a system with a date format set to American MM/DD/YYYY causes obvious errors when the day and month no longer match up.

    There should be no need to worry about date formats unless you are using strings.  The DateTime object is the same in any location - it's only the string representation of a date that changes.  So you need to look for places in your code where you are using a date as a string for any purpose other than showing it to the user (in which case you simply default to whatever format the user has established).  For any other usage you have to be consistent.  For instance, if you are writing to a file as a string, then you need to read back from the file in the same format.  This is what the invariant culture is for - if you set the current culture to invariant before converting that date to/from a string, you will always get the correct conversion.  See:
    https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx#Invariant

    If the problem is occurring because you are allowing the user to type a date into a text box, then you have to redesign the application user interface.

    If the problem is occurring because you are accessing documents that have the date in an unspecified format, then you can try the date time parser:
    https://msdn.microsoft.com/en-us/library/ch92fbc1%28v=vs.110%29.aspx

    However a better solution is to find a way to identify the culture that applies to each specific case, for instance from other information in the document.


    • Edited by Acamar Thursday, May 18, 2017 12:46 AM fmt
    • Marked as answer by AussieHack Thursday, May 18, 2017 12:51 AM
    Thursday, May 18, 2017 12:44 AM
  • Hi Rob,

    In advance, sorry, if I misunderstand your question.

    You can use Format function and make date format as you like.
    Dim today As Date = DateTime.Now
    MessageBox.Show _
       ("Australian: " & Format(today, "dd/MM/yyyy") & vbCrLf _
      & "American:   " & Format(today, "MM/dd/yyyy") & vbCrLf _
      & "Japanese:   " & Format(today, "yyyy/MM/dd"))

    ___________
    Ashidacchi
    • Marked as answer by AussieHack Thursday, May 18, 2017 1:11 AM
    Thursday, May 18, 2017 12:59 AM

All replies

  • Rob,

    It's hard to know what you're dealing with but if you can get to the .Ticks value of an instance of DateTime, that transcends culture and time zone.

    To re-recreate it, use one of the constructors of DateTime:

    Dim dt As New DateTime(ticksValueHere)

    If you then use the .ToString method, you'll see it shown in your culture and time zone and I'll see it in mine.

    I don't know if that helps or not?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, May 18, 2017 12:21 AM
  • thankyou for your reply Charles,

    Actually I may have over thought the issue, Just been playing with it then and it seems if I just run the Date through a function such as

      DateToConvert = DateAndTime.Day(DateToConvert) & "/" & DateAndTime.Month(DateToConvert) & "/" & DateAndTime.Year(DateToConvert)

    seems to work all too well, and I can restructure the format any way I want very easily.

    Would still be good to know how to get the date format preference set by windows though during runtime, if at all possible?

    Am very sorry to waist your time. I think this will work OK for me for now.
    Appreciate your reply though all the same.



    • Edited by AussieHack Thursday, May 18, 2017 12:41 AM
    • Marked as answer by AussieHack Thursday, May 18, 2017 12:50 AM
    Thursday, May 18, 2017 12:41 AM
  • Though running the same code on a system with a date format set to American MM/DD/YYYY causes obvious errors when the day and month no longer match up.

    There should be no need to worry about date formats unless you are using strings.  The DateTime object is the same in any location - it's only the string representation of a date that changes.  So you need to look for places in your code where you are using a date as a string for any purpose other than showing it to the user (in which case you simply default to whatever format the user has established).  For any other usage you have to be consistent.  For instance, if you are writing to a file as a string, then you need to read back from the file in the same format.  This is what the invariant culture is for - if you set the current culture to invariant before converting that date to/from a string, you will always get the correct conversion.  See:
    https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx#Invariant

    If the problem is occurring because you are allowing the user to type a date into a text box, then you have to redesign the application user interface.

    If the problem is occurring because you are accessing documents that have the date in an unspecified format, then you can try the date time parser:
    https://msdn.microsoft.com/en-us/library/ch92fbc1%28v=vs.110%29.aspx

    However a better solution is to find a way to identify the culture that applies to each specific case, for instance from other information in the document.


    • Edited by Acamar Thursday, May 18, 2017 12:46 AM fmt
    • Marked as answer by AussieHack Thursday, May 18, 2017 12:51 AM
    Thursday, May 18, 2017 12:44 AM
  • Thankyou For Reply, What I have Replied Above To Charles Will Work For Me.

    Really appreciate your time to type all that out though. Think I confused myself a bit there... my bad.


    Edit:
    Just to explain, yes I was delimiting the date as a string, clearly this was incorrect.
    Again, my bad.
    • Edited by AussieHack Thursday, May 18, 2017 12:54 AM
    Thursday, May 18, 2017 12:50 AM
  • Hi Rob,

    In advance, sorry, if I misunderstand your question.

    You can use Format function and make date format as you like.
    Dim today As Date = DateTime.Now
    MessageBox.Show _
       ("Australian: " & Format(today, "dd/MM/yyyy") & vbCrLf _
      & "American:   " & Format(today, "MM/dd/yyyy") & vbCrLf _
      & "Japanese:   " & Format(today, "yyyy/MM/dd"))

    ___________
    Ashidacchi
    • Marked as answer by AussieHack Thursday, May 18, 2017 1:11 AM
    Thursday, May 18, 2017 12:59 AM
  • That is very handy, thank you very much. Is sort of what I was expecting to begin with.

    Appreciate your reply

    Thursday, May 18, 2017 1:10 AM
  • thankyou for your reply Charles,



    My name is Frank; the quote is by a person named Charles. ;-)

    *****

    I didn't understand what you were asking so I'm glad to see that you have it all worked out now, but to further embellish it some, you can format a DateTime object many ways and to help that, have a look at the formatting string values that we have available to us:

    Custom DateTime Format Strings

    Also, the DateTime structure itself has a good many "built-in" ways. If you'll have a look at this MSDN document:

    https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

    Look in "Methods" and then scroll down to those that start with "To". You'll see a good many (like .ToShortDateString) that make short work of formatting it.

    *****

    My point was about transmitting an instant in time. Even though we're half the globe away, an instant in time is the same one. It's early morning here and late night there, but it's still the same instant in time.

    I learned that the hard way a few years ago right here. I was working with a woman in New Zealand, writing XML for a project she had. It finally occurred to me to forget all formatting of DateTime and just represent it using the .Ticks value.

    As an example that we can both try, run this please:

    Dim tickValue As Long = 636306912000000000
    
    Dim currentDateTime As New DateTime(tickValue)
    
    Stop

    When it gets to "Stop" it will halt just the same as though you'd put in a breakpoint. When it does, hover your mouse over the variable "currentDateTime". Here that instant in time will show this:

    There it will show differently, but it's still the same instant in time.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, May 18, 2017 8:46 AM