Date methods from recordset

J

John Kenickney

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?
 
R

RobG

John said:
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>
 
J

John Kenickney

RobG said:
John said:
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? :)
 
D

Dr John Stockton

JRS: In article <41d9f6c6$0$31857$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Tue, 4 Jan 2005 11:47:42, seen in

You should have read the newsgroup FAQ, carefully, before posting.
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.

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.


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top