Problem with datetime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Problem with datetime

    Hi all,

    I am using .NET 2

    I have to work with short dates, e.g. 23 April 2008 will come to me as
    23/04/08

    I am splitting this value into int to put into a DateTime type...

    DateTime MyDate = new DateTime(8, 4, 23);

    However, when returned, it is returning as 23/04/0008 (obviously incorrect,
    but the MyDate.WeekdayN ame is returning the correct week day name).

    Later in my code, I have to do a select on a SQL based on the Date, and
    23/04/2008 does not match 23/04/0008


    Now, I can pass in the string direct (23/04/08) into my SQL and that will
    work, however, I want to be ABSOLUTELY certain that say for example, 11 May
    08 won't be interpreted as 5 Nov 08 which is why I am handling it by
    splitting my incoming date string.

    Any ideas on how I can get it to work the way I want?

    --
    Best regards,
    Dave Colliver.

    ~~
    http://www.FOCUSPortals.com - Local franchises available


  • fcs

    #2
    Re: Problem with datetime

    this might help:
    //for date in english:

    using System.Globaliz ation;

    CultureInfo dateSystem = new CultureInfo( "en-US" );


    DateTime mydate = Convert.ToDateT ime(this.txtDat e.Text,dateSyst em);

    now mydate wich is from txtDate entered as :mm/dd/yy is good for SQL

    also, in some point you may need to check the date before you pass it to
    SQL:

    String.Format(" {0}/{1}/{2}", mydate.Month, mydate.Day, mydate.Year);

    Vaf








    "David" <david.colliver .NEWS@revilloc. REMOVETHIS.comw rote in message
    news:e6Pi4QVpIH A.3960@TK2MSFTN GP02.phx.gbl...
    Hi all,
    >
    I am using .NET 2
    >
    I have to work with short dates, e.g. 23 April 2008 will come to me as
    23/04/08
    >
    I am splitting this value into int to put into a DateTime type...
    >
    DateTime MyDate = new DateTime(8, 4, 23);
    >
    However, when returned, it is returning as 23/04/0008 (obviously
    incorrect, but the MyDate.WeekdayN ame is returning the correct week day
    name).
    >
    Later in my code, I have to do a select on a SQL based on the Date, and
    23/04/2008 does not match 23/04/0008
    >
    >
    Now, I can pass in the string direct (23/04/08) into my SQL and that will
    work, however, I want to be ABSOLUTELY certain that say for example, 11
    May 08 won't be interpreted as 5 Nov 08 which is why I am handling it by
    splitting my incoming date string.
    >
    Any ideas on how I can get it to work the way I want?
    >
    --
    Best regards,
    Dave Colliver.

    ~~
    http://www.FOCUSPortals.com - Local franchises available
    >

    Comment

    • Hans Kesting

      #3
      Re: Problem with datetime

      David brought next idea :
      Hi all,
      >
      I am using .NET 2
      >
      I have to work with short dates, e.g. 23 April 2008 will come to me as
      23/04/08
      >
      I am splitting this value into int to put into a DateTime type...
      >
      DateTime MyDate = new DateTime(8, 4, 23);
      Here you are specifying the year 8, instead of 2008. You have to handle
      the conversion to "2008" (maybe by adding 2000, but what if you got
      "99" meaning "1999"?)
      >
      However, when returned, it is returning as 23/04/0008 (obviously incorrect,
      but the MyDate.WeekdayN ame is returning the correct week day name).
      >
      That may break for some other date.
      Later in my code, I have to do a select on a SQL based on the Date, and
      23/04/2008 does not match 23/04/0008
      >
      >
      Now, I can pass in the string direct (23/04/08) into my SQL and that will
      work,
      SqlServer interprets two-digit years below 50 (I think) as 20xx and
      higher values as 19xx. And it will break on years before 1752 or
      thereabouts.
      however, I want to be ABSOLUTELY certain that say for example, 11 May
      08 won't be interpreted as 5 Nov 08 which is why I am handling it by
      splitting my incoming date string.
      >
      Any ideas on how I can get it to work the way I want?
      Do not pass the date as a string. Use a Parameter to pass the value to
      sql and use the (correct) DateTime value directly as the parameter
      value.


      Hans Kesting


      Comment

      • David

        #4
        Re: Problem with datetime

        Hi,

        Thanks for responding...

        Inline...


        "Hans Kesting" <invalid.hansdk @spamgourmet.co mwrote in message
        news:mn.bc237d8 4d29e7466.82533 @spamgourmet.co m...
        David brought next idea :
        >Hi all,
        >>
        >I am using .NET 2
        >>
        >I have to work with short dates, e.g. 23 April 2008 will come to me as
        >23/04/08
        >>
        >I am splitting this value into int to put into a DateTime type...
        >>
        >DateTime MyDate = new DateTime(8, 4, 23);
        >
        Here you are specifying the year 8, instead of 2008. You have to handle
        the conversion to "2008" (maybe by adding 2000, but what if you got "99"
        meaning "1999"?)

        I had considered this, but can't really hard code the 2000. Everything has
        to be as flexible as it can. My Year actually comes in as 08, but the
        DateTime requires it as Int, which changes 08 to 8.

        >
        >>
        >However, when returned, it is returning as 23/04/0008 (obviously
        >incorrect, but the MyDate.WeekdayN ame is returning the correct week day
        >name).
        >>
        >
        That may break for some other date.

        This is what I thought, so, this is why I need to ensure that the date
        coming in is somehow understood as a full year.

        >
        >Later in my code, I have to do a select on a SQL based on the Date, and
        >23/04/2008 does not match 23/04/0008
        >>
        >>
        >Now, I can pass in the string direct (23/04/08) into my SQL and that will
        >work,
        >
        SqlServer interprets two-digit years below 50 (I think) as 20xx and higher
        values as 19xx. And it will break on years before 1752 or thereabouts.

        I sort of worked that out when I passed in my date string.

        >
        >however, I want to be ABSOLUTELY certain that say for example, 11 May 08
        >won't be interpreted as 5 Nov 08 which is why I am handling it by
        >splitting my incoming date string.
        >>
        >Any ideas on how I can get it to work the way I want?
        >
        Do not pass the date as a string. Use a Parameter to pass the value to sql
        and use the (correct) DateTime value directly as the parameter value.
        >

        When I say passing to SQL, it is actually in a DataTable.Selec t() statement,
        and I don't know how to pass this as a parameter.

        I am loading all the dates locally, then when I need to check against a
        date, do a DataTable.Selec t.


        >
        Hans Kesting
        >
        >

        Comment

        Working...