Can I add code that shows the current date in an article?
AnsweredI have an article that is similar to an Application Status Page until we get an automatic one set up. I want to know two date things:
1. Is there a way to show the current date and have it update automatically each day in the article?
2. Is there a way to show previous dates (the previous 5 days) that update automatically?
I have tried a few different HTML codes but they have not worked successfully for me.
Thanks!
-
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_head2. Is there a way to show previous dates (the previous 5 days) that update automatically?
You can manipulate date object and get previous datesRegards,
Sushant -
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
-
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 -
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
-
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. -
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
-
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 -
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
-
Jeanette Hawkinson
Do join google meet if you are available
https://meet.google.com/sep-nqsc-axo -
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
-
I have been trying this solution for API 2 with no luck. Any help with this solution?
-
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 -
Sushant Awalekar is there no other way to do it than JQuery with V2?
-
Sondra Halperin
You can replace jquery statements with vanilla javascript
Replace$('.cur_date').text(current_date);
with
document.querySelector(".cur_date").textContent = current_date;
-
Thanks! That worked great.
Please sign in to leave a comment.
15 Comments