Tech Off Thread

7 posts

help! please! DateTime handling... :((

Back to Forum: Tech Off
  • User profile image
    claxonn

    Hi everyone,

    i'm a programmer from Romania, medium level .. let's say, but since i've started to write code i had a problem... the DateTime type. The problem is that every time it's the discution .. 03.02.2005 means March 2nd 2005, or 3 - february -2005?

    How can write safe code that won't care about the regional settings (of the server, of the local computer)?
     

    One more thing .. i know that TDateTime is a double. 

    So .. any hints from you guys/girls?  TKS.

  • User profile image
    GurliGebis

    I'm not sure what you are asking about.
    But if you are storing a date, I would suggest using this format: YYYYMMDD , since it makes it possible to sort it easy.
    You could also try using unix time (seconds since 1. January 1970 00:00:00 GMT).
    If you where asking for something else, please correct me.

  • User profile image
    claxonn

    Example:

    Now i'm working to an app. developed with MqSQL 4.1.19. Connection: MyODMC. I store a date in the database. When i retrive it .. i try to store in in a string variable. Depending on local setting, the string contains '03.02.2005' or '02/03/2005'. Now .. I want to know if there is a method to handle this kind of situations.

    It's clearer now? TKS.

  • User profile image
    Mike Dimmick

    Call DateTime.ParseExact to convert from a string in a specific format to a date, or call DateTime.ToString with a custom format.

  • User profile image
    claxonn

    That's the problem! I don't want to use a specific format because the server's local setting may vary and it's not a good solution. I want to know if there is a date format standard for developers, for writing code .. you know .. we like precise thing.

    Anybody .. how do you handle situations in witch you must store dates in a MySQL Db.

    If someone has a small routine of doing those thing .. please help me.

    I forgot to mention .. i'm not working now in .NET, the app was written in Borland Delphi 7 and now I must upgrade it.

    TKS!

  • User profile image
    Mike Dimmick

    OK, my first tip is to use the native date/time data type in the database. Not having to do the conversion will help you.

    When I have to represent a date as a string, I try to use the ISO 8601 date format. This is basically YYYY-MM-DD. Microsoft SQL Server always accepts the format YYYYMMDD and always interprets it correctly, regardless of the locale or collation currently selected. MySQL interprets date literals similarly (see the above link).

  • User profile image
    HippieGeek

    If the date is being stored in the database as a date type then you should read that in with a  date object. the date object should adhear to the local settings of the computer it is run on. then when you pass the date back to the database make sure to place it in a date object. that way a date is a date is a date. 

    Hope that was of some help.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.