Show different text -- depends on the current date

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

    #1

    Show different text -- depends on the current date

    Imagine I have a small, static, personal site (I do, actually:
    http://kirpi.it/).
    And imagine that, just below the title of the page, I would like to
    have a comment, or a news line, or a birthday reminder, a citation, or
    whatever you like.
    At the moment you will find a line that reminds about my small child's
    second birthday, just as an example.
    My choice is:
    a) set it manually every day as I wake up, before breakfast, or
    b) have it set by the system, automatically.

    What I thought is: I can write a document, let's call it "facts.txt" ,
    and have it sit somewhere inside my site. Such a page would probably
    look like this, maybe:
    0101 This is the first day of the year.
    0201 Happy Birthday, Dad!
    0301 Travel is fatal to prejudice, bigotry and narrow-mindedness.
    [Mark Twain]
    0401 ...

    Now, my problem: is there a simple way to grab the right line, based
    on the current date, and have it displayed properly?
    Or, failing that, is there a better way you can think of?

    Thanks
    Luigi
  • Martin Honnen

    #2
    Re: Show different text -- depends on the current date



    Luigi wrote:
    [color=blue]
    > Imagine I have a small, static, personal site (I do, actually:
    > http://kirpi.it/).[/color]

    [color=blue]
    > What I thought is: I can write a document, let's call it "facts.txt" ,
    > and have it sit somewhere inside my site. Such a page would probably
    > look like this, maybe:
    > 0101 This is the first day of the year.
    > 0201 Happy Birthday, Dad!
    > 0301 Travel is fatal to prejudice, bigotry and narrow-mindedness.
    > [Mark Twain]
    > 0401 ...[/color]

    Well you could use a script file with an array e.g.
    var quotes = [
    'This is the first day of the year.',
    'Happy Birthday, Dad!',
    'Travel is fatal to prejudice, bigotry and narrow-mindedness.'
    ];
    include that in the HTML page
    <script type="text/javascript" src="quotes.js" ></script>
    and then have another script find the current date
    var today = new Date();
    and use the methods of the Date object to find the right index in the
    array and output the quote.
    Or put an object into the file e.g.
    var quotes = {
    '0101': 'This is the first day of the year.',
    '0201': ''Happy Birthday, Dad!',
    }
    that might be easier to organize.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Andrew Thompson

      #3
      Re: Show different text -- depends on the current date

      On 30 Nov 2004 08:59:47 -0800, Luigi wrote:

      (random quote)[color=blue]
      > look like this, maybe:
      > 0101 This is the first day of the year.
      > 0201 Happy Birthday, Dad!
      > 0301 Travel is fatal to prejudice, bigotry and narrow-mindedness.
      > [Mark Twain]
      > 0401 ...
      >
      > Now, my problem: is there a simple way to grab the right line, based
      > on the current date, and have it displayed properly?[/color]

      If you store the information as a JS array, it becomes much
      simpler to extract the information and present it in a page.

      --
      Andrew Thompson
      http://www.PhySci.org/codes/ Web & IT Help
      http://www.PhySci.org/ Open-source software suite
      http://www.1point1C.org/ Science & Technology
      http://www.LensEscapes.com/ Images that escape the mundane

      Comment

      • Luigi

        #4
        Re: Show different text -- depends on the current date

        > If you store the information as a JS array, it becomes much[color=blue]
        > simpler to extract the information and present it in a page.[/color]

        Andrew and Martin converge to this point: I'd better store the whole
        list as an array.
        Actually, I run a small site just because I have something to share,
        but I am not a computer expert, and hardly understand what an array
        is. I will study it, if the need comes. But there are two things that
        are not clear to me, yet:
        a) can I store the list in a *separate* document, not to burden too
        much every single page with the same, stupid bulk of lines? Or am I
        forced to repeat on every single page of the site the same whole list?
        b) How really, practically, can I get a single phrase printed on the
        page?

        Maybe some of the answers were somehow present in your prompt [thank
        you!] replies, but you both speak a sort of technical jargon with very
        specialized terms, and I could not properly understand everything.

        Luigi

        Comment

        • Mick White

          #5
          Re: Show different text -- depends on the current date

          Luigi wrote:

          [snip][color=blue]
          >
          > Maybe some of the answers were somehow present in your prompt [thank
          > you!] replies, but you both speak a sort of technical jargon with very
          > specialized terms, and I could not properly understand everything.[/color]

          for instance:

          <script>
          q=
          ["1130 This is the first day of the year.",
          "1201 Happy Birthday, Dad!",
          "1202 Travel is fatal to prejudice, bigotry and narrow-mindedness.-Mark
          Twain"]

          function getToday(){
          var t=new Date();
          var m=t.getMonth()+ 1;
          var d=t.getDate();
          m=m<10?"0"+m:m;
          d=d<10?"0"+d:d;
          return ""+m+d;//US convention
          }

          function getQuote(){
          var qq=q.length
          while(qq--){
          if(q[qq].substring(0,4) ==getToday()){
          return q[qq].substring(5);
          }
          }
          return "";
          }
          document.write( getQuote())
          </script>
          Mick

          Comment

          • Andrew Thompson

            #6
            Re: Show different text -- depends on the current date

            On 30 Nov 2004 16:19:28 -0800, Luigi wrote:
            [color=blue]
            > a) can I store the list in a *separate* document, not to burden too
            > much every single page with the same, stupid bulk of lines? Or am I
            > forced to repeat on every single page of the site the same whole list?[/color]

            Mick seems to have covered b), so I'll just add that you can put
            the JS (the data and functions) in a separate file and include it
            like this..

            <head>
            ....
            <script src='/qtdata.js' type='text/javascript'></script>
            </head>
            <body>
            ....

            ...then in your page, you might call Mick's function like this

            <script type='text/javascript'>
            getQuote();
            </script>

            HTH

            --
            Andrew Thompson
            http://www.PhySci.org/codes/ Web & IT Help
            http://www.PhySci.org/ Open-source software suite
            http://www.1point1C.org/ Science & Technology
            http://www.LensEscapes.com/ Images that escape the mundane

            Comment

            • Luigi

              #7
              Re: Show different text -- depends on the current date

              Your answers look quite interesting!
              I will try them as soon as I have some spare time, and will let you
              know.
              What can I give back to the community for such a kind help? Maybe
              nothing technical, I'm afraid. Anyway I just posted one of small son's
              smiling images, in case somebody likes it: http://kirpi.it/.
              Luigi

              Comment

              • Luigi

                #8
                Re: Show different text -- depends on the current date

                I tried and follow you.
                I now have http://kirpi.it/qtdata.js, where Mick's file is saved. I
                set data this way:
                1201 for the first day of December
                1202 for the second day of December
                1203 for the first day of December
                This is maybe the way American adopt; I read something inside Mick's
                file which was about some "US convention".

                Then I pasted Andrew's suggestions in my page [http://kirpi.it], where
                <script type='text/javascript'>
                getQuote();
                </script>
                is about the bottom of the page.

                Sad news: I hoped to see something, as today is here the second day of
                December.
                But nothing gets written on the screen.

                Luigi

                Comment

                • Mick White

                  #9
                  Re: Show different text -- depends on the current date

                  Luigi wrote:
                  [color=blue]
                  > Your answers look quite interesting!
                  > I will try them as soon as I have some spare time, and will let you
                  > know.
                  > What can I give back to the community for such a kind help? Maybe
                  > nothing technical, I'm afraid. Anyway I just posted one of small son's
                  > smiling images, in case somebody likes it: http://kirpi.it/.
                  > Luigi[/color]
                  Replace this:
                  <script type='text/javascript'>
                  getQuote();
                  </script>

                  with this:
                  <script type='text/javascript'>
                  document.write( getQuote());
                  </script>
                  Mick

                  Comment

                  • Mick White

                    #10
                    Re: Show different text -- depends on the current date

                    Luigi wrote:
                    [color=blue]
                    >
                    > Then I pasted Andrew's suggestions in my page [http://kirpi.it], where
                    > <script type='text/javascript'>
                    > getQuote();
                    > </script>
                    > is about the bottom of the page.
                    >
                    > Sad news: I hoped to see something, as today is here the second day of
                    > December.
                    > But nothing gets written on the screen.
                    >[/color]


                    See my previous post.
                    Mick

                    Comment

                    • Dr John Stockton

                      #11
                      Show different text -- depends on the current date

                      JRS: In article <f64531ff.04113 01619.654b745@p osting.google.c om>, dated
                      Tue, 30 Nov 2004 16:19:28, seen in news:comp.lang. javascript, Luigi
                      <kirpi@kirpi.it > posted :
                      [color=blue][color=green]
                      >> If you store the information as a JS array, it becomes much
                      >> simpler to extract the information and present it in a page.[/color]
                      >
                      >Andrew and Martin converge to this point: I'd better store the whole
                      >list as an array.[/color]

                      You do better to store it as an Object, because it then has the
                      semblance of direct access. Martin suggested that.

                      var X = {
                      '0101': 'This is the first day of the year.',
                      '0102': 'Happy Birthday, Dad!',
                      '0103': 'Travel is fatal ... [Mark Twain]',
                      '1202': '...',
                      Dummy:'' }

                      with (new Date())
                      S = String(10100+ge tMonth()*100+ge tDate()).substr ing(1)
                      Y = X[S] // apparent direct access


                      X has that layout for convenient auto-editing of an existing list; all
                      data lines have the same punctuation.

                      The date-of-year should be rendered as MMDD, in accordance with ISO 8601
                      and with reason.

                      The whole of the above can be put in a *.js Include file, invoked like
                      <script type="text/javascript" src="include1.j s"></script>
                      in each page; each page then "knows" Y.


                      It's an amusing exercise; but it means that every visitor, even if on
                      dial-up or slow radio link, is forced to load all the data. You could
                      perhaps have a different data file for each month, and compute the
                      include file name for the current month ... . ISTM a waste.

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                      Comment

                      • Andrew Thompson

                        #12
                        Re: Show different text -- depends on the current date

                        On Thu, 02 Dec 2004 00:15:08 GMT, Mick White wrote:
                        [color=blue]
                        > document.write( getQuote());[/color]

                        Oh, right.. (slaps forehead) I saw a document.write( ) up *near*
                        the function and mistook it for being *inside* the function,
                        but the way you explain it makes a great deal more sense.
                        (For example, why you called the method 'getQuote' rather than
                        'writeQuote' - I must pay more attention in future)

                        --
                        Andrew Thompson
                        http://www.PhySci.org/codes/ Web & IT Help
                        http://www.PhySci.org/ Open-source software suite
                        http://www.1point1C.org/ Science & Technology
                        http://www.LensEscapes.com/ Images that escape the mundane

                        Comment

                        • Dr John Stockton

                          #13
                          Show different text -- depends on the current date

                          JRS: In article <f64531ff.04120 11540.467c5f3a@ posting.google. com>,
                          dated Wed, 1 Dec 2004 15:40:38, seen in news:comp.lang. javascript, Luigi
                          <kirpi@kirpi.it > posted :
                          [color=blue]
                          > I
                          >set data this way:
                          >1201 for the first day of December
                          >1202 for the second day of December
                          >1203 for the first day of December
                          >This is maybe the way American adopt; I read something inside Mick's
                          >file which was about some "US convention".[/color]

                          Not specifically US. In writing the date in FFF, with the year at the
                          wrong end, the Americans write dates such that if the year is omitted
                          what remains complies inadvertently, no doubt) with the international
                          standard ISO 8601. The ISO standard order is Y M D - biggest first, just
                          as in h:m:s or dollars & cents or £ s d or Lire & centimos or euros &
                          cents.

                          Traditional custom outside US-afflicted regions is, in the rational Far
                          East, to precede ISO in using Y M D; and elsewhere, merely to reverse
                          the order to D M Y, as is common in postal addresses.

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                          Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                          PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                          Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                          Comment

                          • kirpi@kirpi.it

                            #14
                            Re: Show different text -- depends on the current date

                            So I tried, but it seems that I did something wrong: nothing shows on
                            my home page [http://kirpi.it]. You all can check the page source and
                            the extra file [http://kirpi.it/qtdata.js].
                            I do not understand.

                            [color=blue][color=green][color=darkred]
                            > >> If you store the information as a JS array, it becomes much
                            > >> simpler to extract the information and present it in a page.[/color][/color]
                            >
                            > You do better to store it as an Object, because it then has the
                            > semblance of direct access. Martin suggested that.
                            >
                            > var X = {
                            > '0101': 'This is the first day of the year.',
                            > '0102': 'Happy Birthday, Dad!',
                            > '0103': 'Travel is fatal ... [Mark Twain]',
                            > '1202': '...',
                            > Dummy:'' }
                            >
                            > with (new Date())
                            > S = String(10100+ge tMonth()*100+ge tDate()).substr ing(1)
                            > Y = X[S] // apparent direct access
                            >
                            > X has that layout for convenient auto-editing of an existing list;[/color]
                            all[color=blue]
                            > data lines have the same punctuation.
                            >
                            > The whole of the above can be put in a *.js Include file, invoked[/color]
                            like[color=blue]
                            > <script type="text/javascript" src="include1.j s"></script>
                            > in each page; each page then "knows" Y.[/color]

                            John, first of all thank you for the infos about dates.
                            As for your lines above, I must confess that they are like chinese to
                            me: as you tell about "objects", "auto-editing" and the like,...
                            If I can find something that work, I will adopt it, but completely
                            trust in you all about the technical side of it.
                            What exactly should I paste, please?

                            [color=blue]
                            > It's an amusing exercise; but it means that every visitor, even if on
                            > dial-up or slow radio link, is forced to load all the data. You[/color]
                            could[color=blue]
                            > perhaps have a different data file for each month, and compute the
                            > include file name for the current month ... . ISTM a waste.[/color]

                            What do you mean?
                            The whole thing is an amusing exercise, or your last "object"
                            suggestion is?
                            The idea to break the big file into smaller ones could be interesting,
                            but not faisible by me.
                            "ISTM"? Please, realize that my mother tongue is not english, sorry.
                            What is a waste, then?
                            Luigi

                            Comment

                            Working...