value-of var into a string

A

Andrew Neiderer

I am using the Google AJAX search API for simple queries. I want to do so
only for the current day. Right now I have

var date = new Date();
var today = date.toString();
document.write(today.toDateString());

which prints

Wed Jan 31 2007

I'd like it to look like "Jan 31, 2007" before I query Google, ie
eliminate "Wed" and add a comma after 31. Can someone tell me which
combination of Date methods to use and how?

Also I want the value-of of the string in a searchString; "today" and
not the actual value is used below. Can someone also tell me how
to get to the value of the var.

var searchString = "al-Sadr OR Al-Sadr OR ('Mahdi' AND 'Army') AND today";

Thank you.
 
R

Randy Webb

Andrew Neiderer said the following on 1/31/2007 5:37 AM:
I am using the Google AJAX search API for simple queries. I want to do so
only for the current day. Right now I have

var date = new Date();
var today = date.toString();
document.write(today.toDateString());

which prints

Wed Jan 31 2007

I'd like it to look like "Jan 31, 2007" before I query Google, ie
eliminate "Wed" and add a comma after 31. Can someone tell me which
combination of Date methods to use and how?

You will need to write your own routine to do it.

Search the archives for "format date" and you can find many examples on
how to get it in the format you want.

<FAQENTRY>
How do I format a Date Object?
</FAQENTRY>

John, do you have a page, or partial page, that covers date formatting
that could be put in an entry?
Also I want the value-of of the string in a searchString; "today" and
not the actual value is used below. Can someone also tell me how
to get to the value of the var.

var searchString = "al-Sadr OR Al-Sadr OR ('Mahdi' AND 'Army') AND today";

Take it out of the quotes and append it:

var searchString = "al-Sadr OR Al-Sadr OR ('Mahdi' AND 'Army') AND " +
today;

That should come out as one line but if it doesn't then it all needs to
be on one line of code.
 
R

RobG

Andrew said:
I am using the Google AJAX search API for simple queries. I want to do so
only for the current day. Right now I have

var date = new Date();
var today = date.toString();
document.write(today.toDateString());

which prints

Wed Jan 31 2007

I'd like it to look like "Jan 31, 2007" before I query Google, ie
eliminate "Wed" and add a comma after 31. Can someone tell me which
combination of Date methods to use and how?

There are no built-in methods to do what you want, so you can either use
a library or write your own function to do it. Since what you are after
is fairly simple, try:

function googleDate(date){
date = date || new Date();
var month = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
return month[date.getMonth()] + ' ' +
date.getDate() + ', ' + date.getFullYear();
}

// Today's date
alert(googleDate());

// A specific date
alert(googleDate( new Date('2004/02/29')) );


If you don't pass it a date object, it uses the current system date. A
lot of information about the javascript date object (and working with
dates in general) is here:

Also I want the value-of of the string in a searchString; "today" and
not the actual value is used below. Can someone also tell me how
to get to the value of the var.

var searchString = "al-Sadr OR Al-Sadr OR ('Mahdi' AND 'Army') AND today";

var searchString = "al-Sadr ... AND " + today;


You will find it very helpful to read the FAQ:

<URL: http://www.jibbering.com/faq/ >
 
R

RobG

Am 31.01.2007 um 00:22 schrieb RobG:



They could have used Java in the first place, no need to "invent" a
new language.

JavaScript was invented to be a simple, forgiving language to be used
by web developers, not hard-core programmers. It succeeded wildly.

<URL: http://www.mozilla.org/js/language/ICFP-Keynote.ppt >

Brendan Eich has long lamented that the name was changed to
"JavaScript" - it's nothing like Java and was never meant to be.
Browser vendors are free to implement any language engine in their
browser they like - IE allows VBScript, there are others - but you've
got to ask why was JavaScript so stupidly popular?
I guess that's just the NIH thing big companies have. :)

I don't think that was it - JavaScript is nearly perfect for its
intended use. It filled a void and is now an indispensable part of
most web sites. That JavaScript 2.0 has languished for over 5 years
is testimony that it just isn't needed.

MS is an American company, not British ;)

Ah, too subtle by half. :)
 
M

marss

Andrew said:
var today = date.toString();
document.write(today.toDateString());

which prints

Wed Jan 31 2007

Hi,
I never head about the toDateString method, maybe it is lack of
knowledge :).
Usual the toString method of the Date object returns longer string:
Wed Jan 31 16:12:01 UTC+0200 2007
You can use a regular expression to get required format.

var date = new Date();
today = date.toString().replace(/\w+\s+(\w+\s+\d+).*(\s+\d
+)/,"$1,$2");

It also works if you have "Wed Jan 31 2007" string.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
I never head about the toDateString method, maybe it is lack of
knowledge :).

Not in IE4; in IE6.
Usual the toString method of the Date object returns longer string:
Wed Jan 31 16:12:01 UTC+0200 2007

That format is, IIRC, NOT specified by ECMA; toString can do whatever
it wants.

If the browser writer has gone for maximum localisation, then in
France tomorrow.toDateString() *might* give Jeu 1 Fev 2007 or
maybe 2007 Fev 01, Jeu . Or maybe .toLocaleDateString() would.


Granted the OP is .army.mil ; but allies might not be set for Anglo
conventions.
You can use a regular expression to get required format.

Txt = new Date().toDateString().replace... will be simplest, but only
reliable in controlled circumstances.

OP: It's a good idea to read the newsgroup and its FAQ. See below.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
<FAQENTRY>
How do I format a Date Object?
</FAQENTRY>

John, do you have a page, or partial page, that covers date formatting that could be put in an entry?

If you read the FAQ on the subject of dates - Section 3 - you will not
need to ask such questions. I support mainly ISO standard formats; but
the material can be adapted for local preferences such as FFF.

<URL:http://www.merlyn.demon.co.uk/js-dates.htm> ff.; - p.7 is for
week-dates.

Page "Javascript Date and Time 9 : Output Formatting" is relevant,
but did not bother much about anything so simple.

= = =

<FAQENTRY> ISTM that the FAQ could now refer more to Wikipedia. For
example, considering the recent posting of the DOM section, 2.9?, I
remark that, while the W3 references you give are potent & definitive,
they are only partially scrutable when first seen, and the Wiki article,
though a stub, if read in parallel or first, could aid understanding.

<li>Wikipedia :-<ul>
<li><a href="http://en.wikipedia.org/wiki/Javascript">JavaScript</a>
<li><a href="http://en.wikipedia.org/wiki/JavaScript_syntax">JavaScript syntax</a>
<li><a href="http://en.wikipedia.org/wiki/Document_Object_Model">Document Object Model</a>
<li><i>etc.</i>
</ul>
 
M

marss

Dr said:
That format is, IIRC, NOT specified by ECMA; toString can do whatever
it wants.

If the browser writer has gone for maximum localisation, then in
France tomorrow.toDateString() *might* give Jeu 1 Fev 2007 or
maybe 2007 Fev 01, Jeu . Or maybe .toLocaleDateString() would.


Granted the OP is .army.mil ; but allies might not be set for Anglo
conventions.


Txt = new Date().toDateString().replace... will be simplest, but only
reliable in controlled circumstances.

OP: It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. [email protected] Turnpike v6.05 IE 6
FAQ <URL:http://www.jibbering.com/faq/index.html>.
<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.

Hello, John
I have read news group FAQ a year ago. There are many useful tips and
tricks but it is not a Bible. I'm not going to reread it every time I
want to post my suggestion about a specific topic. If I have some
doubt I prefer to turn to the origins:

http://msdn.microsoft.com
http://developer.mozilla.org/en/docs/Main_Page
http://www.ecma-international.org/publications/standards/Ecma-262.htm

If I am mistaken, what of it? To err is human. Thought thrives on
conflict and not on reading FAQ.
Summary. Thanks for the sensible comment on my post. No thanks for the
obtrusive boost your or your friend's sites.
Regards,
Mykola
 
L

-Lost

This way, you would have known to snip irrelevant content. Like John's signature below.

Hello, John
I have read news group FAQ a year ago. There are many useful tips and
tricks but it is not a Bible. I'm not going to reread it every time I
want to post my suggestion about a specific topic.

Just for the record, all modern browsers have a search in page feature. Normally CTRL+F
fires it. In that regard you could have easily searched for just what you need.

I personally have not read the entire FAQ (does me no good to read it all in one sitting),
but I search it frequently.

And wait a minute... you are not the OP anyway.
If I have some doubt I prefer to turn to the origins:
http://msdn.microsoft.com
http://developer.mozilla.org/en/docs/Main_Page
http://www.ecma-international.org/publications/standards/Ecma-262.htm

If I am mistaken, what of it? To err is human. Thought thrives on
conflict and not on reading FAQ.
Summary. Thanks for the sensible comment on my post.
No thanks for the obtrusive boost your or your friend's sites.

Um, that is a rather weak argument, accusing him of advertising or spam (or whatever they
call it nowadays). Articles that point to the most commonly issued questions and/or
resources/references is always a welcome item.

You will not find the same in any of the above URLs you mentioned (despite their awesome
use as a resource).

Just my $0.02.

-Lost
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top