Appending Date/Time Stamp to filename

Discussion in 'Javascript' started by Dr John Stockton, Sep 26, 2004.

  1. JRS: In article <>, dated
    Sun, 26 Sep 2004 07:04:22, seen in news:comp.lang.javascript, Don
    <> posted :

    >function datetimestamp()
    >{
    > var today = new Date();
    > var sToday = (today.getMonth()+1).toString();
    > sToday += today.getDate().toString();
    > sToday += today.getYear().toString();
    > sToday += today.getHours().toString();
    > sToday += today.getMinutes().toString();
    > sToday += today.getSeconds().toString();
    > return sToday;
    >}


    The concatenation is more effectively done as

    var sToday = "" +
    today.getMonth()+1) +
    today.getDate() +
    today.getYear() +
    today.getHours() +
    today.getMinutes() +
    today.getSeconds() ;

    Unless you use field separators or leading zeroes or both, the result
    will be horribly ambiguous if M<10 xor D<10, or any but not all of h m s
    <10 (that assumes the year to be recognisable).

    FFF is unwise for general use; follow FIPS 4.1 (if American) or ISO
    8601.

    The following gives a universally comprehensible result, and, after
    thinking about it for the first time, will seem far simpler.

    with (new Date()) sToday = ((((
    getYear()*100 + (getMonth()+1))*100 + getDate())*100 +
    getHours())*100 + getMinutes())*100 + getSeconds()

    For leading zeroes AND separators, use <FAQENTRY>

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


    *** DO NOT MULTI-POST ***


    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    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 John Stockton, Sep 26, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVydA==?=

    Date/Time stamp in SQL

    =?Utf-8?B?QmVydA==?=, Jan 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    21,443
    Wardeaux
    Jan 30, 2004
  2. srini
    Replies:
    9
    Views:
    11,576
    Beauregard T. Shagnasty
    Apr 14, 2004
  3. mark | r

    time and date stamp - start stop button?

    mark | r, Nov 30, 2005, in forum: ASP General
    Replies:
    3
    Views:
    151
    mark | r
    Nov 30, 2005
  4. Brave
    Replies:
    3
    Views:
    847
    Brave
    Jul 25, 2009
  5. flyboynm
    Replies:
    0
    Views:
    308
    flyboynm
    Nov 17, 2013
Loading...

Share This Page