javascript equivalent for vbscript Date()-1

Discussion in 'Javascript' started by shelleybobelly, Mar 28, 2007.

  1. 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?
    shelleybobelly, Mar 28, 2007
    1. Advertisements

  2. shelleybobelly

    Ivo Guest

    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 );
    That 's a terribly good question. I guess the answer includes the
    flexibility that Javascript offers by leaving all formatting up to us.
    Ivo, Mar 28, 2007
    1. Advertisements

  3. shelleybobelly

    Ivo Guest

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

    Ivo, Mar 28, 2007
  4. or
    var x = new Date();
    var s = x.getFullYear() + '/' + x.getMonth() + '/' + (x.getDate()-1);
    alert( s );
, Mar 28, 2007
  5. shelleybobelly

    Rick Brandt Guest

    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?
    Rick Brandt, Mar 28, 2007
  6. In comp.lang.javascript message <>, Wed, 28 Mar 2007 09:23:55, shelleybobelly
    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

    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()) }
    Dr J R Stockton, Mar 28, 2007
  7. In comp.lang.javascript message <460aa3b6$0$31467$.
    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.

    <> 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.
    Dr J R Stockton, Mar 28, 2007
  8. shelleybobelly

    Ivo Guest

    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
    (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:
    Ivo, Mar 29, 2007
  9. shelleybobelly

    Evertjan. Guest

    Ivo wrote on 29 mrt 2007 in comp.lang.javascript:
    nice touch, that regex, however the global flag is missing:

    s = s.join( '-' ).replace( /\b(\d)\b/g, '0$1' );
    Evertjan., Mar 29, 2007
  10. In comp.lang.javascript message <460b0dea$0$3900$
    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.
    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.

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

    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.
    Dr J R Stockton, Mar 29, 2007
  11. <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 G Harris, Mar 29, 2007
  12. shelleybobelly

    Lee Guest

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

    Lee, Mar 29, 2007
  13. shelleybobelly

    Lee Guest

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

    Lee, Mar 30, 2007
  14. shelleybobelly

    Ivo Guest

    Interesting. The updated page will certainly benefit from these hints.
    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
    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 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.
    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:
    Like you say.
    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.
    Ivo, Mar 30, 2007
  15. shelleybobelly

    Matt Kruse Guest

    How does a site acquire this "authority" anyway? If your site is referenced,
    do you also state that it has no authority?
    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.
    Matt Kruse, Mar 30, 2007
  16. In comp.lang.javascript message <460c6770$0$24797$.
    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 ?

    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).
    Dr J R Stockton, Mar 30, 2007
  17. 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 G Harris, Mar 30, 2007
  18. shelleybobelly

    Lee Guest

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

    Lee, Mar 30, 2007
  19. In comp.lang.javascript message <830F0FF37FB96852AD0
    8924D9443D28E23ED5CD>, Fri, 30 Mar 2007 21:12:54, John G Harris
    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.
    Dr J R Stockton, Mar 30, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.