Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / March 2007



Tip: Looking for answers? Try searching our database.

javascript equivalent for vbscript Date()-1

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shelleybobelly - 28 Mar 2007 17:23 GMT
I'm looking to return DATE ONLY for yesterday's date. No seconds,
milliseconds. Formatted either yyyy/mm/dd or mm/dd/yyyy. VB does it so
easily Date()-1 will return 03/27/2007 if today is 03/28/2007. Why so
many hoops for javascript? Any ideas?
Ivo - 28 Mar 2007 18:13 GMT
> I'm looking to return DATE ONLY for yesterday's date. No seconds,
> milliseconds. Formatted either yyyy/mm/dd or mm/dd/yyyy.

Never use the latter format. It 's highly confusing to a worldwide audience.

var x = new Date();
var s = x.getFullYear() + '/' + x.getMonth() + '/' + x.getDate();
alert( s );

> VB does it so
> easily Date()-1 will return 03/27/2007 if today is 03/28/2007. Why
> so many hoops for javascript? Any ideas?

That 's a terribly good question. I guess the answer includes the
flexibility that Javascript offers by leaving all formatting up to us.
hth
ivo
http://4umi.com/web/javascript/ref.htm#date
Ivo - 28 Mar 2007 18:19 GMT
"Ivo" <no@thank.you> schreef
> "shelleybobelly" <shelleybobelly@yahoo.com> schreef
>> I'm looking to return DATE ONLY for yesterday's date.

I forgot to substract a day, one more line of code:

var x = new Date();
x.setDate( x.getDate() - 1 );
var s = x.getFullYear() + '/' + x.getMonth() + '/' + x.getDate();
alert( s );

hth
ivo
http://4umi.com/web/javascript/ref.htm#date
scripts.contact - 28 Mar 2007 20:39 GMT
> I forgot to substract a day, one more line of code:
>
> var x = new Date();
> x.setDate( x.getDate() - 1 );
> var s = x.getFullYear() + '/' + x.getMonth() + '/' + x.getDate();
> alert( s );

or
var x = new Date();
var s = x.getFullYear() + '/' + x.getMonth() + '/' + (x.getDate()-1);
alert( s );
Rick Brandt - 28 Mar 2007 20:44 GMT
>> I forgot to substract a day, one more line of code:
>>
[quoted text clipped - 7 lines]
> var s = x.getFullYear() + '/' + x.getMonth() + '/' + (x.getDate()-1);
> alert( s );

But getMonth() is zero-based so you'd have to add 1 correct?  And what will
getDate()-1 do on the first of the month?  Won't you get zero?
Randy Webb - 28 Mar 2007 22:41 GMT
Rick Brandt said the following on 3/28/2007 3:44 PM:
>>> I forgot to substract a day, one more line of code:
>>>
[quoted text clipped - 9 lines]
>
> But getMonth() is zero-based so you'd have to add 1 correct?  

Did you test it?

> And what will getDate()-1 do on the first of the month?  

It will return a result that will probably surprise you if you test it
in several browsers.

> Won't you get zero?

Did you test it? <g>

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Rick Brandt - 29 Mar 2007 02:59 GMT
> Rick Brandt said the following on 3/28/2007 3:44 PM:
> > > > I forgot to substract a day, one more line of code:
[quoted text clipped - 12 lines]
>
> Did you test it?

Testing it in Firebug I get 2 which is last month (in non-js-speak).

> > And what will getDate()-1 do on the first of the month?
>
[quoted text clipped - 4 lines]
>
> Did you test it? <g>

I get "2007/2/0".  What am I missing?
Dr J R Stockton - 28 Mar 2007 23:38 GMT
In comp.lang.javascript message <460aa3b6$0$31467$dbd49001@news.wanadoo.
nl>, Wed, 28 Mar 2007 19:19:49, Ivo <no@thank.you> posted:
>"Ivo" <no@thank.you> schreef
>> "shelleybobelly" <shelleybobelly@yahoo.com> schreef
[quoted text clipped - 6 lines]
>var s = x.getFullYear() + '/' + x.getMonth() + '/' + x.getDate();
>alert( s );

To look inept, a good way is to fail to test the code you propose.

Firstly, if run today that gives 2007/2/27 and on Saturday it should
give 2007/2/30.  February 30th is uncommon.

Secondly, in all-numeric dates the month and date fields should always
be extended to 2 characters; today is 2007 03 28.

Thirdly, to be fully standard the separators should be "-", though
javascript cannot be relied on to read that form, preferring 2007/03/28.

<http://4umi.com/web/javascript/ref.htm#date> is a copy (probably) of an
imprecise source, and should not be recommended.

But the worldclock on that site is interesting; it seems to assume that
the state of Summer Time changes everywhere at the same instant as at
the user's location.

It's a good idea to read the newsgroup c.l.j and its FAQ.  See below.

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6
news:comp.lang.javascript 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.

Ivo - 29 Mar 2007 01:52 GMT
"Dr J R Stockton" <jrs@merlyn.demon.co.uk> schreef
> But the worldclock on that site is interesting; it seems to assume that
> the state of Summer Time changes everywhere at the same instant as at
> the user's location.

Thank you for your feedback. That reference page is in dire need of an
update, and has been for a while. It is made from a database full of
oneliner descriptions, which was itself in fact put together for other
purposes. And the whole site is in a state of constant improvement,
so it is only good to find more room for that :-) If you are the one who
shows up in my log as searching for "email", try 4umi.com/contact
(between you and me; it 's a well hidden page).

So, with your comments in mind, the code is now:
var x = new Date();
x.setDate( x.getDate() - 1 );
var s = [ x.getFullYear(), x.getMonth() + 1, x.getDate() ];
s = s.join( '-' ).replace( /\b(\d)\b/, '0$1' );
alert( s );

BTW, using a "with" block as in another branch in this thread seems
not very good practice:
http://www.javascripttoolbox.com/bestpractices/#with

Signature

Regards, Ivo,   homing in on
http://4umi.com/web/javascript/worldclock.htm

Evertjan. - 29 Mar 2007 12:10 GMT
Ivo wrote on 29 mrt 2007 in comp.lang.javascript:

> "Dr J R Stockton" <jrs@merlyn.demon.co.uk> schreef
> > But the worldclock on that site is interesting; it seems to assume that
[quoted text clipped - 14 lines]
> var s = [ x.getFullYear(), x.getMonth() + 1, x.getDate() ];
> s = s.join( '-' ).replace( /\b(\d)\b/, '0$1' );

nice touch, that regex, however the global flag is missing:

s = s.join( '-' ).replace( /\b(\d)\b/g, '0$1' );

> alert( s );
>
> BTW, using a "with" block as in another branch in this thread seems
> not very good practice:
> http://www.javascripttoolbox.com/bestpractices/#with

Signature

Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Dr J R Stockton - 29 Mar 2007 19:58 GMT
In comp.lang.javascript message <460b0dea$0$3900$dbd4f001@news.wanadoo.n
l>, Thu, 29 Mar 2007 02:52:57, Ivo <no@thank.you> posted:
>"Dr J R Stockton" <jrs@merlyn.demon.co.uk> schreef
> > But the worldclock on that site is interesting; it seems to assume that
[quoted text clipped - 5 lines]
>oneliner descriptions, which was itself in fact put together for other
>purposes.

The reference page is a mere copy of other defective material; it would
be easy to improve the one-liners of the Date Object.

For example, all references to 1970 need to include a UTC, and
"midnight" should not be used.  "UTC 1970.0" is exact, and I don't see
how anyone intelligent enough to program successfully can fail to
understand it.  Or "1970-01-01 00:00:00 UTC".

Method getFullYear does not return a 4-digit year - try
new Date(-5e13).getFullYear() and new Date(5e15).getFullYear() - it
returns a Number.

The worldclock page is best deleted, as a precaution against anyone
believing it.

> And the whole site is in a state of constant improvement,
>so it is only good to find more room for that :-) If you are the one who
>shows up in my log as searching for "email", try 4umi.com/contact
>(between you and me; it 's a well hidden page).

Probably me; I was in fact trying to find out who was responsible for
the site.  One should never pay much attention to technical documents
that do not declare their authorship and give some indication of age.

>var s = [ x.getFullYear(), x.getMonth() + 1, x.getDate() ];
>s = s.join( '-' ).replace( /\b(\d)\b/, '0$1' );

That is appreciably slower to run than
function Lz(x) { return (x<10?"0":"") + x }
s = x.getFullYear() + "-" + Lz(x.getMonth()+1) + "-" + Lz(x.getDate()) ;

>BTW, using a "with" block as in another branch in this thread seems
>not very good practice:
>http://www.javascripttoolbox.com/bestpractices/#with

That site has no real authority; and the argument in #with is not
entirely convincing when carefully examined.

If "with" is used wantonly and there are errors in the code, it can
indeed be difficult to see what is happening.

But when there is no global date object, and the "with" clearly uses a
Date Object, and the identifiers in the statement are not going to have
other meanings, using it can add no problems.

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6.
Web  <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Ivo - 30 Mar 2007 02:27 GMT
"Dr J R Stockton" <jrs@merlyn.demon.co.uk> schreef
>>, Thu, 29 Mar 2007 02:52:57, Ivo <no@thank.you> posted:
>>"Dr J R Stockton" <jrs@merlyn.demon.co.uk> schreef
[quoted text clipped - 6 lines]
> new Date(-5e13).getFullYear() and new Date(5e15).getFullYear() - it
> returns a Number.

Interesting. The updated page will certainly benefit from these hints.

> The worldclock page is best deleted, as a precaution against anyone
> believing it.

Anyone blindly believing anything only deserves so, especially on the www.
People go online for information, not for knowledge. The page may be full of
inaccuracies, it may be one big lie (as was the page that I originally
copied), but it can satisfy much curiosity, ignite even more, and it may
spark a thought in one visitor about the wonders of our world. In that I
find satisfaction, until of course you convince me of some real evil in the
page.

> Probably me; I was in fact trying to find out who was responsible for
> the site.  One should never pay much attention to technical documents
> that do not declare their authorship and give some indication of age.

My picture of you has always been someone in his fifties, I cannot say why,
I don't remember such indication being prominently advertised on your site,
I must say. And if I told you I happen to turn 36 today, and gave you an url
such as http://www.vansandick.com/familie/kalender/?20070330 to make it look
convincing, wouldn't that be reason enough to be suspicious? Then why should
credibility of any text depend on the name under it? What matters is what
works. Especially technical texts are easy to judge by empirically trying
out the claims that are made. Experiment is key. That 's how information
becomes knowledge. Any text that shows me something I haven't tested yet, is
worthwhile, and I find that the older I get, more and more of such texts are
written by nameless youths.

>>BTW, using a "with" block as in another branch in this thread seems
>>not very good practice:
>>http://www.javascripttoolbox.com/bestpractices/#with
>
> That site has no real authority; and the argument in #with is not
> entirely convincing when carefully examined.

I 'm not building on their authority, just gave a pointer. The argument
could be expressed stronger, shall we say "The problem is that the
programmer has no way to verify that input1 or input2 are actually being
resolved as properties of the form elements array" is a bit long and
winding, but it stands solid. I don't think there are words that will
entirely convince you. Inside the 'with' block, there is no way to tell an
independent input2 from a 'with'ed input2. There is a ghost host object at
every level in the scope chain. Ay, there 's the rub. There is no defense
against the ambiguity created with 'with'. I also refer to a contemporary
thread started by Rasmus Kromann-Larsen, 'Eliminating "with"...', especially
the very first paragraph: "I'm currently writing a master thesis on static
analysis of JavaScript, and after investigating the with statement, it only
[became] even more evident to me that the with statement is indeed bad." See
for the arguments:
http://groups.google.nl/group/comp.lang.javascript/browse_thread/thread/a953bd62
1436d8d3/f877b6f155fd916a


> If "with" is used wantonly and there are errors in the code, it can
> indeed be difficult to see what is happening.

Like you say.

> But when there is no global date object, and the "with" clearly
> uses a Date Object, and the identifiers in the statement are not
> going to have other meanings, using it can add no problems.

With all conditions met, I probably am with you on the Date object argument,
but I maintain that continuing to use 'with' sustains a flaw in the
language. We 're really better off without.
Signature

ever underage
Ivo
http://www.trust.onlyfools.com/

Dr J R Stockton - 30 Mar 2007 13:29 GMT
In comp.lang.javascript message <460c6770$0$24797$dbd4f001@news.wanadoo.
nl>, Fri, 30 Mar 2007 03:27:09, Ivo <no@thank.you> posted:
>"Dr J R Stockton" <jrs@merlyn.demon.co.uk> schreef
>>>, Thu, 29 Mar 2007 02:52:57, Ivo <no@thank.you> posted:
[quoted text clipped - 9 lines]
>
>Interesting. The updated page will certainly benefit from these hints.

They are but examples, of course.  You might say that all Date Methods
return Numbers, except where otherwise stated.  Change GMT to UTC, or
use GMT/UTC.  getTimezoneOffset() returns minutes, (UTC-local).  setYear
presumes 1900-1999 if given 0-99, but not otherwise.

Method toLocaleString gives local time, not local zone.  I guess you are
a Dutch resident; if so, your Zone is always UTC+1 but your time in
Summer is UTC+2.

Date.UTC is missing?  valueOf is not there.

Math.Random does not return a random number between 0 and 1; it can
return 0.0 but should not return 1.0.

Math.round - rounding of x.5 needs mention.

String lacks charCodeAt ?

<http://4umi.com/web/javascript/clock.htm>   seems broken (XP sp2 IE6).

>> Probably me; I was in fact trying to find out who was responsible for
>> the site.  One should never pay much attention to technical documents
>> that do not declare their authorship and give some indication of age.

> ... ...

> Any text that shows me something I haven't tested yet, is
>worthwhile, and I find that the older I get, more and more of such texts are
>written by nameless youths.

The name as an identifier of a real tangible person is not important; I
can only recall three or four named authors in News or Web who I've
personally met.  But the presence of a full name in most cases allows
all the News/Web works of an author to be considered as a whole, and
indicates that the Net personality is willing to take responsibility for
the work and to risk his personal friends finding his Net work.

It can also be useful to be able to identify for preservation the works
of the recently deceased.

IMHO, the amount of interesting stuff on the Net that one has not yet
personally tested is so great that an indication of trustworthiness,
even if itself not entirely trustworthy, is useful.

If one recalls that someone, say me, has written something relevant,
then one can Google for the name and topic.  Google reports 21,600,000
hits on Ivo, 1.270,000 for Ivo nl, but only 8 for "Ivo Tromp" (the
second Dutch surname that sprang to mind).

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   MIME.
Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Matt Kruse - 30 Mar 2007 03:27 GMT
>> BTW, using a "with" block as in another branch in this thread seems
>> not very good practice:
>> http://www.javascripttoolbox.com/bestpractices/#with
> That site has no real authority;

How does a site acquire this "authority" anyway? If your site is referenced,
do you also state that it has no authority?

> and the argument in #with is not
> entirely convincing when carefully examined.

The word "avoid" is used instead of "don't ever use" because there are
always exceptions.
I personally feel that there is no convincing argument to ever use the
'with' statement, even where it will work correctly. There are other, less
error-prone, more readable ways to achieve the same result.

Signature

Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com

John G Harris - 29 Mar 2007 20:04 GMT
 <snip>
>Secondly, in all-numeric dates the month and date fields should always
>be extended to 2 characters; today is 2007 03 28.
 <snip>

What nonsense. If I want my next birthday to be displayed as 29/7/2007
then that's what the programmer should damn-well code otherwise he won't
get paid and that will make him unhappy and I don't care.

 John
Signature

John Harris

Lee - 29 Mar 2007 20:33 GMT
John G Harris said:

>  <snip>
>>Secondly, in all-numeric dates the month and date fields should always
[quoted text clipped - 4 lines]
>then that's what the programmer should damn-well code otherwise he won't
>get paid and that will make him unhappy and I don't care.

The job market must be different in your part of the world, if you can
impose your whims over good practices.

--
Randy Webb - 29 Mar 2007 23:33 GMT
Lee said the following on 3/29/2007 3:33 PM:
> John G Harris said:
>>
[quoted text clipped - 9 lines]
> The job market must be different in your part of the world, if you can
> impose your whims over good practices.

Then re-word it slightly:

If the boss wants the date to be displayed as 7/29/2007 then that's what
the programmer should damn-well code otherwise he won't get paid (and
probably get fired).

Or, is the job market in your part of the world where you can dictate to
your boss how you should write/code/display dates/data on a page?

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Lee - 30 Mar 2007 00:07 GMT
Randy Webb said:

>Lee said the following on 3/29/2007 3:33 PM:
>> John G Harris said:
[quoted text clipped - 19 lines]
>Or, is the job market in your part of the world where you can dictate to
>your boss how you should write/code/display dates/data on a page?

If he makes bad decisions and won't listen to reason (or refuses to
explain his reasons), I'll go work for somebody else.  He knows that,
and would rather compromise than have to replace me (or anyone else).

--
John G Harris - 30 Mar 2007 21:12 GMT
>Randy Webb said:
>>
[quoted text clipped - 25 lines]
>explain his reasons), I'll go work for somebody else.  He knows that,
>and would rather compromise than have to replace me (or anyone else).

Who said anything about bosses? I'm a customer. If a software company or
freelance programmer won't write my party invitations the way I want
them written then they can buzz off and I'll find somebody else to do
the job.

In case you're still wondering, this is about customer requirements.
There are contexts where redundant zeroes in dates just aren't
acceptable. Saying you must *always* format a date in a particular way
regardless of context is wrong.

If you insist on displaying dates in a fixed punched card format then to
be logical you should cater for all the year numbers that javascript can
handle. Today is 0002007-03-30, I think.

 John

Signature

John Harris

Lee - 30 Mar 2007 23:27 GMT
John G Harris said:

>>Randy Webb said:
>>>
[quoted text clipped - 39 lines]
>be logical you should cater for all the year numbers that javascript can
>handle. Today is 0002007-03-30, I think.

Nothing at all logical about that.  You seem to be talking about
graphical design, not programming, so you're welcome to your whims.

--
Dr J R Stockton - 30 Mar 2007 23:46 GMT
In comp.lang.javascript message <htGwLQJG9WDGFw8X@J.A830F0FF37FB96852AD0
8924D9443D28E23ED5CD>, Fri, 30 Mar 2007 21:12:54, John G Harris
<john@nospam.demon.co.uk> posted:

>If you insist on displaying dates in a fixed punched card format then to
>be logical you should cater for all the year numbers that javascript can
>handle. Today is 0002007-03-30, I think.

The javascript range is +- 10^8 days from UTC 1970.0, so you have there
a digit more than your argument justifies.

Today should, in agreement with ISO 8601:2004(E), be given as
2007-03-30; the standard has provision for extension of YYYY.  But it
will no doubt be updated before AD 9999.

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6.
Web  <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Dr J R Stockton - 28 Mar 2007 23:35 GMT
In comp.lang.javascript message <1175099035.174150.224030@e65g2000hsc.go
oglegroups.com>, Wed, 28 Mar 2007 09:23:55, shelleybobelly
<shelleybobelly@yahoo.com> posted:
>I'm looking to return DATE ONLY for yesterday's date. No seconds,
>milliseconds. Formatted either yyyy/mm/dd or mm/dd/yyyy. VB does it so
>easily Date()-1 will return 03/27/2007 if today is 03/28/2007.

No; it is important to understand the language properly.  In VB, Date or
Date() returns a value of type CDate with an integer value representing
the number of days from 1899-12-30 local = 0.

Then the default conversion to CStr gives the value in the date form set
as default in the operating system.  In most places that will be either
of D M Y form or of Y M D form, but Americans prefer Fred Flintstone
Format.

>Why so
>many hoops for javascript? Any ideas?

VBscript tends to provide a simple approach for the commonest business
need; javascript provides a set of primitives on which the programmer
can build.

The following will (except possibly once a year, for an hour, in the
Azores or thereabouts) return a local date string in the proper
international form, e.g. "2007-03-27".

function Lz(x) { return (x<10&&x>=0?"0":"") + x }

with (new Date()) { setDate(getDate()-1) ;
 Str = getFullYear() + "-" + Lz(getMonth()+1) + "-" + Lz(getDate()) }

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6.
Web  <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.