Coding Compendium
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.
This is part of the Javascript Smart Marketing Toolkit.
The date handling features of the toolkit can be used to automate seasonal promotions and weekly promotions.
They can also be used to add a bit more life to your copy, by making it respond to the time.
A time-responsive message like "Why not come down tomorrow - we're open until 10pm on a Saturday and it's not too late to phone us today to make a reservation" becomes easy to program. Site visitors can focus on what's relevant now instead of scanning pages of text for the same information.
The time and date used are those set on the visitor's computer, except that the countdown uses Universal Time Coordinated (UTC) time for the dates. Roughly speaking, this is UK time. Given that the countdown is working in days (rather than hours or minutes), this may be accurate enough for use in many other countries.
function | what it does | example code |
---|---|---|
the_hour() | Returns the hour from the time now (24 hour clock) | if (the_hour()<12) {document.write("Good morning!");} else if (the_hour()<18) {document.write("Good afternoon!");} else document.write("Good evening!"); |
the_minute() | Returns the minute from the time now | See example for the_second() |
the_second() | Returns the second from the time now | document.write("The time is ",the_hour(),":", the_minute(),":",the_second()); |
the_date() | Returns today's date | document.write("Today's date:",day_name(the_day())," ",the_date()," ",month_name(the_month())); |
the_day() | Returns the day of the week, where 0 is Sunday and 6 is Saturday | if (the_day()==6 || the_day()==0) { document.write("Spoil yourself this weekend!"); } else document.write("Relax - you've been working hard today"); |
the_year() | Returns the year. I needed this for some of the internal code, so I'm exposing it here. I'm not sure how useful it is, compared to the other functions. | document.write(the_year()); |
day_name() | Returns the name of a given day, where 0 is Sunday and 6 is Saturday | document.write("Today is ",day_name(the_day())); |
the_month() | Returns the month number, where 0 is January and 11 is December | if (the_month()==11) {document.write("Seasons greetings!");} |
month_name() | Returns the name of a given month, where 0 is January and 11 is December | document.write("This ",month_name(the_month())," why not take a weekend break?"); |
countdown(year, month, date, event description, message after event happens ) | Displays the number of days remaining until a given date (example shown is for a book being published on 27th July 2020). Dates use conventional numbering (1 is January, 12 is December). On day 0, the post-event message is shown. This is good for counting down to a product launch. | countdown(2020, 7, 27, "to publication day!", "This book is out now!"); |
countdown_recurring(month, date, event description, message for day of event ) | Displays the number of days remaining until the next occurrence of a given date (example shown is for Christmas Day). Dates use conventional numbering (1 is January, 12 is December). On day 0, the message for the day of the event is shown. This is good for counting down to anniversaries, birthdays, and holidays that have the same date each year. Using it, you can include a counter on your website that does not need modifying each year. | countdown_recurring(12,25, "to Christmas Day!", "It's Christmas Day! Happy Christmas!"); |
See the page on banner cycling features of the toolkit for more information on what this is doing and how it works.
© Sean McManus. All rights reserved.
Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.
This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.
Code a space adventure game in this Python programming book published by No Starch Press.
Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!