Date as Filename

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Smoke

    Date as Filename

    I have a series of pictures that are named as dates, example November 10th,
    2003 would be 10102003.gif

    Why does the following script fail?

    <script language="JavaS cript">

    var d = new Date()
    var filename = (d.getDate()) & (d.getMonth() + 1)& (d.getFullYear( ))
    var fileextension = ".gif"
    var file = filename + fileextension
    document.write( "<img src=http://www.mysite.com/fishing/"+file+" width='358'
    height='233' border='0' alt='' >")

    </script>


  • VK

    #2
    Re: Date as Filename

    Welcome from xBasic to JavaScript ! :-)

    1. & is bitwise "AND" operator (not a string concatenator)
    2. ends of statements marked by (not just by new line)

    Good luck!

    var d = new Date();
    var filename = "" + (d.getDate()) + (d.getMonth() + 1) +
    (d.getFullYear( ));
    var fileextension = ".gif";
    var file = filename + fileextension;
    document.write( "<img src=http://www.mysite.com/fishing/"+file+"
    width='358'
    height='233' border='0' alt='' >");


    Comment

    • VK

      #3
      Re: Date as Filename

      and yes, you have to use "pseudo-casting" with an empty string (there
      are more options):
      "" + (d.getDate()) + ...
      in order to receive the concatenated string and not the sum of
      arguments.


      Comment

      • Dr John Stockton

        #4
        Re: Date as Filename

        JRS: In article <wZMrb.13583$62 .6872@lakeread0 4>, seen in
        news:comp.lang. javascript, Smoke <smoke@xatrium. com> posted at Mon, 10
        Nov 2003 08:08:29 :-[color=blue]
        >I have a series of pictures that are named as dates, example November 10th,
        >2003 would be 10102003.gif[/color]

        Never use the Nth day of the month numbered N as an example; number
        months from 0 only internally, and not in public. Best to use day-of-
        month > 12 in an example. Date fields should be in Y M D order, so that
        Christmas coming is 20031225. In the US 10102003 means October 10th
        2003; but elsewhere it means 10th October 2003.

        [color=blue]
        >Why does the following script fail?
        >
        ><script language="JavaS cript">
        >
        >var d = new Date()
        > var filename = (d.getDate()) & (d.getMonth() + 1)& (d.getFullYear( ))[/color]

        & != +

        The smart way to get YYYYMMDD is

        var fn = (d.getFullYear( )*100 + d.getMonth()+1) *100 + d.getDate()

        Note that you seem to have forgotten to provide leading zeroes.

        However, to be similar to your example the +1 should not be used.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        • Fabian

          #5
          Re: Date as Filename

          Dr John Stockton hu kiteb:

          [color=blue]
          > The smart way to get YYYYMMDD is
          >
          > var fn = (d.getFullYear( )*100 + d.getMonth()+1) *100 + d.getDate()[/color]

          year * 10000


          --
          --
          Fabian
          Visit my website often and for long periods!


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Date as Filename

            "Fabian" <lajzar@hotmail .com> writes:
            [color=blue]
            > Dr John Stockton hu kiteb:
            >
            >[color=green]
            > > The smart way to get YYYYMMDD is
            > >
            > > var fn = (d.getFullYear( )*100 + d.getMonth()+1) *100 + d.getDate()[/color]
            >
            > year * 10000[/color]

            No, notice the parentheses and the second *100.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Dr John Stockton

              #7
              Re: Date as Filename

              JRS: In article <u15b7gbm.fsf@h otpop.com>, seen in
              news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
              posted at Tue, 11 Nov 2003 04:08:13 :-[color=blue]
              >"Fabian" <lajzar@hotmail .com> writes:
              >[color=green]
              >> Dr John Stockton hu kiteb:
              >>
              >>[color=darkred]
              >> > The smart way to get YYYYMMDD is
              >> >
              >> > var fn = (d.getFullYear( )*100 + d.getMonth()+1) *100 + d.getDate()[/color]
              >>
              >> year * 10000[/color]
              >
              >No, notice the parentheses and the second *100.[/color]

              Notice also that in my method each operator can be executed immediately
              it is reached, whereas with 10000 that would not be the case; mine is
              therefore better on a single-accumulator pseudo-machine.

              I don't know whether that helps, but it is unlikely to be harmful.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk / ??.Stockton@phy sics.org ©
              Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
              Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
              Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

              Comment

              Working...