javascript equivalent for vbscript Date()-1

S

shelleybobelly

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

Ivo

shelleybobelly said:
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
 
S

scripts.contact

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

Rick Brandt

scripts.contact said:
or
var x = new Date();
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?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Wed, 28 Mar 2007 09:23:55, shelleybobelly
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()) }
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected].
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 );

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.
 
I

Ivo

Dr J R Stockton said:
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
 
E

Evertjan.

Ivo wrote on 29 mrt 2007 in comp.lang.javascript:
Dr J R Stockton said:
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' );

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

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

Dr J R Stockton

In comp.lang.javascript message <[email protected]
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.

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.
 
J

John G Harris

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
 
L

Lee

John G Harris said:
<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.

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


--
 
L

Lee

Randy Webb said:
Lee said the following on 3/29/2007 3:33 PM:

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?

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


--
 
I

Ivo

Dr J R Stockton said:
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.

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.
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....read/thread/a953bd621436d8d3/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.
 
M

Matt Kruse

Dr said:
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.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected].
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 ?

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

John G Harris

Randy Webb said:

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

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
 
L

Lee

John G Harris said:
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.

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


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
8924D9443D28E23ED5CD>, Fri, 30 Mar 2007 21:12:54, John G Harris
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.
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top