Date methods from recordset

Discussion in 'Javascript' started by John Kenickney, Jan 3, 2005.

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

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

    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").toLocaleString()

    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").value
    var strDate = theDate.toLocaleString()

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

    Any ideas?
     
    John Kenickney, Jan 3, 2005
    #1
    1. Advertising

  2. John Kenickney

    RobG Guest

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


    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)

    > theDate = rs.Fields.Item("date").value


    What does 'rs.Fields.Item("date").value' return? If it's a
    string, you just turned theDate into a string and it's not a date
    object any more.

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

    [...]

    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.getFullYear();
    var month = +theDate.getMonth()+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.

    > Any ideas?


    Read Dr. J's stuff here:

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


    --
    Rob
     
    RobG, Jan 4, 2005
    #2
    1. Advertising

  3. "RobG" <> schreef in bericht
    news:41d9f6c6$0$31857$...
    > John Kenickney wrote:
    > > I use ASP/Javascript. I pull a date from a recordset field like this.
    > >
    > > var theDate = new Date()

    >
    > 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)
    >
    > > theDate = rs.Fields.Item("date").value

    >
    > What does 'rs.Fields.Item("date").value' return? If it's a
    > string, you just turned theDate into a string and it's not a date
    > object any more.
    >
    > >
    > > Now I want to apply the method toLocaleString(). There are 2 things I

    can
    > > think of

    > [...]
    >
    > 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.getFullYear();
    > var month = +theDate.getMonth()+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.
    >
    > > Any ideas?

    >
    > Read Dr. J's stuff here:
    >
    > <URL:http://www.merlyn.demon.co.uk/js-dates.htm>



    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? :)
     
    John Kenickney, Jan 4, 2005
    #3
  4. JRS: In article <41d9f6c6$0$31857$5a62ac22@per-qv1-newsreader-
    01.iinet.net.au>, dated Tue, 4 Jan 2005 11:47:42, seen in
    news:comp.lang.javascript, RobG <> posted :
    >John Kenickney wrote:
    >> I use ASP/Javascript. I pull a date from a recordset field like this.


    You should have read the newsgroup FAQ, carefully, before posting.

    >> var theDate = new Date()

    >
    > 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)


    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.

    >> theDate = rs.Fields.Item("date").value


    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.


    >> Now I want to apply the method toLocaleString().


    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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Jan 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    646
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,364
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    566
    Pete Becker
    Nov 8, 2003
  4. Hung Huynh
    Replies:
    8
    Views:
    347
    Bob Barrows
    Sep 24, 2003
  5. Kenneth McDonald
    Replies:
    5
    Views:
    375
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page