Can I add code that shows the current date in an article?

Respondida

12 Comentários

  • Sushant Awalekar
    Community Moderator

    Hi Jeanette Hawkinson

    1. Is there a way to show the current date and have it update automatically each day in the article?
    Yes, you can show the current date using javascript date objects             (new Date())

     1. Add empty html element in your article

         for ex. <p class="cur_date"></p>

    2. Declare variable to store date from date object
       var  current_date = new Date();
    3. Return the date content to the selected element
      $('.cur_date').text(current_date);

    PS: Add Jquery CDN into your document_head

    2. Is there a way to show previous dates (the previous 5 days) that update automatically?
     You can manipulate date object and get previous dates

     

     

     

    Regards,
    Sushant

     

    0
  • Jeanette Hawkinson

    Sushant Awalekar,

    Thanks for that information.  I tried what you suggested with no luck.  I added the Jquery CDN to the document_head.hbs.  But when I added the rest to my article, I ended up with a blank line where the date should have appeared.  

    Here is the code I entered:

    <p id="cur_date"></p>
    <p>
    <script>
    var current_date = new Date(); $('.cur_date').text(current.date);
    </script>

    Any thoughts?

    Many thanks,

    Jeanette

     

    0
  • Sushant Awalekar
    Community Moderator

    Hi Jeanette Hawkinson

    1. add <p id="cur_date"></p> in your article source code 


    2. Add below code in script.js
       var current_date = new Date();
       $('.cur_date').text(current.date);



    Let me know if this works for you.


    Regards,
    Sushant

    0
  • Jeanette Hawkinson

    Sushant Awalekar,

    Thank you again for the more detailed information.  I think I may know the problem as I am still not getting it to work.  When I look at your screenshot for script.js, it shows Templating API v2.  We are on Custom Theme (1.0.0) Templating API v1.  Will this work on v1?

     

    Thank you for working on this with me.

    Jeanette

    0
  • Sushant Awalekar
    Community Moderator

    Jeanette Hawkinson

    I think if you are using Templating API v1 then there is no need to include jquery externally.(you can exclude the jquery cdn added in document_head.hbs).It should work on v1 otherwise.


    let me know if this works.


    Regards,
    Sushant.


    0
  • Jeanette Hawkinson

    Sushant Awalekar

    I refuse to give up!  I still can't get it to work.

    I removed the jquery cdn from the document_head.hbs

     

    I have the <p id="cur_date"></p> code in the article source code

     

    Then the

       var current_date = new Date();
       $('.cur_date').text(current.date);

    in the script.js

     

    Many thanks for your help,

    Jeanette

    0
  • Sushant Awalekar
    Community Moderator

    Jeanette Hawkinson

    Strange that it's not working for you.

    Upon checking the screenshot you shared. I see some difference in variable declaration and variable you are referring to get the date value in script.js and hence javascript is unable to get value for you. please make sure you refer to the same variable.
    Regards,
    Sushant

    0
  • Jeanette Hawkinson

    Sushant Awalekar

    Thanks for catching that.  I did a straight copy and paste from the code you recommended. :-)  Is there a preference on which to use?  I tried both and still no luck.  I might table this and do some more research over the weekend to see what I can find. :-)

     

    Thanks for all your help and for catching that!

    Jeanette

    0
  • Sushant Awalekar
    Community Moderator

    Jeanette Hawkinson

    Do join google meet if you are available
    https://meet.google.com/sep-nqsc-axo

    0
  • Jeanette Hawkinson

    Sushant Awalekar

    Thanks for meeting with me!  

    The resolution made so much sense once you showed it to me. :-)

    For V1, the code had to go before the end brackets in the script.js page

     

    We also needed to change the <p id in the Source code in the Article to <p class.

     

    I appreciate all the time you spent working with me on this!!

    Jeanette

    0
  • Ginger Collison

    I have been trying this solution for API 2 with no luck. Any help with this solution?

    0
  • Sushant Awalekar
    Community Moderator

    Hi Ginger Collison

    With V2, you can no longer use jQuery statements anywhere in a theme and statements in above solution requires jquery so Please check if you have added Jquery CDN into your document_head? 

    If in case you haven't added jquery please check this article. 
    Importing or upgrading jQuery


    Hope this helps!!


    -sushant

    0

Por favor, entrar para comentar.

Powered by Zendesk