Date methods from recordset

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

    Date methods from recordset

    I use ASP/Javascript. I pull a date from a recordset field like this.

    var theDate = new Date()
    theDate = rs.Fields.Item( "date").val ue

    Now I want to apply the method toLocaleString( ). There are 2 things I can
    think of

    1) change my code to
    var theDate = new Date()
    theDate = rs.Fields.Item( "date").toLocal eString()

    But this gives the error that this method is not supported by that object.

    2) Add a string variable

    var theDate = new Date()
    theDate = rs.Fields.Item( "date").val ue
    var strDate = theDate.toLocal eString()

    Now I get the error message that theDate is null or not an object.

    Any ideas?


  • RobG

    #2
    Re: Date methods from recordset

    John Kenickney wrote:[color=blue]
    > I use ASP/Javascript. I pull a date from a recordset field like this.
    >
    > var theDate = new Date()[/color]

    So 'theDate' contains the date of midnight on the morning of the
    current date according to the user's computer (which may be set
    incorrectly and almost certainly not accurately)
    [color=blue]
    > theDate = rs.Fields.Item( "date").val ue[/color]

    What does 'rs.Fields.Item ("date").val ue' return? If it's a
    string, you just turned theDate into a string and it's not a date
    object any more.
    [color=blue]
    >
    > Now I want to apply the method toLocaleString( ). There are 2 things I can
    > think of[/color]
    [...]

    Neither of which work (but you knew that). If you want to create
    a date for a specific date (say 3 January 2004), use:

    var theDate = new Date(2004,0,3);
    alert(theDate);

    Which, for someone living in Japan, may return something like:

    Sat Jan 03 2004 00:00:00 GMT+1000

    Depending on your OS, browser, settings, etc. If you want a
    decent output format, you must do it yourself, e.g. for an
    ISO8601 date generated according to the users' local settings:

    <script type="text/javascript">
    function addZero(a) {
    return (a < 10)? a = '0'+a : a;
    }

    var theDate = new Date('2004','0' ,'3');
    var year = theDate.getFull Year();
    var month = +theDate.getMon th()+1;
    var daydate = theDate.getDate ();
    alert(year
    + '-' + addZero(month)
    + '-' + addZero(daydate ));
    </script>

    Note that months Jan to Dec are numbered 0 to 11 respectively.
    The addZero() function just adds a leading zero to single digit
    months or days. Some also recommend using getYear(), but it has
    it's own issues. Read below.
    [color=blue]
    > Any ideas?[/color]

    Read Dr. J's stuff here:

    <URL:http://www.merlyn.demo n.co.uk/js-dates.htm>


    --
    Rob

    Comment

    • John Kenickney

      #3
      Re: Date methods from recordset


      "RobG" <rgqld@iinet.ne t.auau> schreef in bericht
      news:41d9f6c6$0 $31857$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...[color=blue]
      > John Kenickney wrote:[color=green]
      > > I use ASP/Javascript. I pull a date from a recordset field like this.
      > >
      > > var theDate = new Date()[/color]
      >
      > So 'theDate' contains the date of midnight on the morning of the
      > current date according to the user's computer (which may be set
      > incorrectly and almost certainly not accurately)
      >[color=green]
      > > theDate = rs.Fields.Item( "date").val ue[/color]
      >
      > What does 'rs.Fields.Item ("date").val ue' return? If it's a
      > string, you just turned theDate into a string and it's not a date
      > object any more.
      >[color=green]
      > >
      > > Now I want to apply the method toLocaleString( ). There are 2 things I[/color][/color]
      can[color=blue][color=green]
      > > think of[/color]
      > [...]
      >
      > Neither of which work (but you knew that). If you want to create
      > a date for a specific date (say 3 January 2004), use:
      >
      > var theDate = new Date(2004,0,3);
      > alert(theDate);
      >
      > Which, for someone living in Japan, may return something like:
      >
      > Sat Jan 03 2004 00:00:00 GMT+1000
      >
      > Depending on your OS, browser, settings, etc. If you want a
      > decent output format, you must do it yourself, e.g. for an
      > ISO8601 date generated according to the users' local settings:
      >
      > <script type="text/javascript">
      > function addZero(a) {
      > return (a < 10)? a = '0'+a : a;
      > }
      >
      > var theDate = new Date('2004','0' ,'3');
      > var year = theDate.getFull Year();
      > var month = +theDate.getMon th()+1;
      > var daydate = theDate.getDate ();
      > alert(year
      > + '-' + addZero(month)
      > + '-' + addZero(daydate ));
      > </script>
      >
      > Note that months Jan to Dec are numbered 0 to 11 respectively.
      > The addZero() function just adds a leading zero to single digit
      > months or days. Some also recommend using getYear(), but it has
      > it's own issues. Read below.
      >[color=green]
      > > Any ideas?[/color]
      >
      > Read Dr. J's stuff here:
      >
      > <URL:http://www.merlyn.demo n.co.uk/js-dates.htm>[/color]


      Thanks for your reply. I must deal however with the info in the recordset so
      I cannot declare a date with the new Date(some date) function. I think I
      must downsize my problem how to get a date object from my recordset. Server
      side is ASP and I use an MS access database which I approach with SQL.

      Maybe I'm in the wrong place here? :-)


      Comment

      • Dr John Stockton

        #4
        Re: Date methods from recordset

        JRS: In article <41d9f6c6$0$318 57$5a62ac22@per-qv1-newsreader-
        01.iinet.net.au >, dated Tue, 4 Jan 2005 11:47:42, seen in
        news:comp.lang. javascript, RobG <rgqld@iinet.ne t.auau> posted :[color=blue]
        >John Kenickney wrote:[color=green]
        >> I use ASP/Javascript. I pull a date from a recordset field like this.[/color][/color]

        You should have read the newsgroup FAQ, carefully, before posting.
        [color=blue][color=green]
        >> var theDate = new Date()[/color]
        >
        > So 'theDate' contains the date of midnight on the morning of the
        > current date according to the user's computer (which may be set
        > incorrectly and almost certainly not accurately)[/color]

        For me, and IIRC ECMA-262, it will contain the current instant.
        Midnight does not occur on a morning; it is between p.m. and a.m. in the
        American dialect, and elsewhere is 00:00 of one day and 24:00 of the
        previous day.
        [color=blue][color=green]
        >> theDate = rs.Fields.Item( "date").val ue[/color][/color]

        Variable theDate was previously set to a Date Object, and it is now set
        to (probably) a string. So (a) it was a waste of time setting it at
        all; (b) if a date object, but not the current instant, were really
        needed, then it is better to use new Date(0), for two reasons.

        [color=blue][color=green]
        >> Now I want to apply the method toLocaleString( ).[/color][/color]

        You no longer have a Date Object; you have a String which may look like
        a date.

        IMHO, toLocaleString cannot normally be used for showing dates to
        ordinary people on a WWW page; the result will confuse many of them.

        One must construct a string that everyone will understand.

        The machines in our (British) Public Library are, or at least were,
        configured to American format and used mainly by Koreans.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...