• 0

Quick question: How do I [javascript]?


Question

So I'm making a website and in one of the section I want a simple layout as you can see here:

 

post-69305-0-02124800-1422301525.png

 

I'm pretty sure I need to use javascript to vary the responses "today is (day)" and "10am til when". The responses are as follows:

 

Monday - Friday

10am til when

 

Saturday

9am til bike

 

Sunday

Maybe, maybe not

 

Can anyone write a simple script that would accomplish this, or point me in the right direction to do this?

Link to comment
https://www.neowin.net/forum/topic/1244664-quick-question-how-do-i-javascript/
Share on other sites

10 answers to this question

Recommended Posts

  • 0
var d = new Date();
var n = d.getDay()
if(n == 6) {
    document.getElementById("lol").innerHTML = "9am til bike";
} else if(!n) {
    document.getElementById("lol").innerHTML = "Maybe, maybe not";
} else {
    document.getElementById("lol").innerHTML = "10 am til when";
}
<p id="lol"></p>

This js code should do the job, keep in mind it does not take the possibility that your visitor is from a different timezone into account.

  • 0
  On 27/01/2015 at 07:47, Muessig said:

Wow thanks! This is mainly for a uk-based client, but for the sake of curiosity how would I alter this to take time zones into account?

var today = new Date();  
var localoffset = -(today.getTimezoneOffset()/60);
var destoffset = -4; 

var offset = destoffset-localoffset;
var d = new Date( new Date().getTime() + offset * 3600 * 1000)
Modify -4 till it shows the correct timezone.
  • 0

Javascript's Date.getDay() will return the date based on the current local time of the browser's computer. If you want to display the GMT day no matter what timezone the browser's computer thinks it's in, you can use Date.getUTCDay(), which takes the browser's local time and adds/subtracts hours based on the timezone setting of the browser's computer to get GMT. Note that both methods are still calculated based on the current date/time/timezone settings of the browser's computer... so if the computer is set to the wrong date or the wrong timezone, both getUTCDay() and getDay() could return incorrect days. It's probably not a big deal for your situation, but it's something to keep in mind.

 

If you're building the website using a server-side language (PHP, ASP.NET, etc), most have built-in date methods that you can use. Calculating the date on the server means that the server date/time is used instead of whatever date/time the browser's computer is set to. This gives you a little more control if you need it.

  • Like 1
  • 0
  On 27/01/2015 at 13:56, briley said:

Javascript's Date.getDay() will return the date based on the current local time of the browser's computer. If you want to display the GMT day no matter what timezone the browser's computer thinks it's in, you can use Date.getUTCDay(), which takes the browser's local time and adds/subtracts hours based on the timezone setting of the browser's computer to get GMT. Note that both methods are still calculated based on the current date/time/timezone settings of the browser's computer... so if the computer is set to the wrong date or the wrong timezone, both getUTCDay() and getDay() could return incorrect days. It's probably not a big deal for your situation, but it's something to keep in mind.

 

If you're building the website using a server-side language (PHP, ASP.NET, etc), most have built-in date methods that you can use. Calculating the date on the server means that the server date/time is used instead of whatever date/time the browser's computer is set to. This gives you a little more control if you need it.

I indeed server side is the way to go if you want to be totally sure it's the same everywhere.

I also looked at getUTCDay but I had some trouble finding out how to change this utc date to +x hours for a specific timezone so that's why I used the normal date function instead.

  • 0

I must be being pretty stupid here guys, but I've included the code and can't get it to display on the page at all. I think I need one bit of code that will say what day it is "Wednesday" or whatever and I need this to just display in a way that I can format it. I then need the second bit of code, which you've provided for me above but I'm not sure what I'm doing wrong...

 

post-69305-0-18521500-1422379744.png

 

The image is washed out because dreamweaver is in the middle of crashing (stupid program) and I've added in the text "today is (DAY)" and "10am til when" myself so I could roughly format the rest of that section.

  • 0

The page is "read" from the top down, and inline javascript like you have is run as soon as it's read, so <p id="lol"> doesn't exist on the page yet when your javascript runs. You'll need to move the javascript after the element you want to modify (putting javascript tags right before your closing </body> tag is one way to ensure everything else has been loaded first). Javascript libraries like jQuery also have helper functions to delay running code until the entire HTML file has been read.

 

For your other question: you can use the same Date.getUTCDate() call to help you figure out what to print in the "today is (TODAY)" space. Consider it your homework!

Hint: The link has an example that should be very useful.

  • 0
  On 27/01/2015 at 18:55, perochan said:

one rule of thumb is

 

put all script file (js or etc.) or inline js codes after all your HTML codes but before </body> tag.

Or use jquery - $(document).ready(function () { });

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • At least on Mint... Debian is the 'backup' option (i.e. LMDE6) where as regular Mint is Ubuntu. also, Debian tends to play stuff a bit more conservatively than Ubuntu (at least based on Mint) with kernel and programs. also, even Mint's 'Update Manager' is more refined in regular Mint vs LMDE6 and installing NVIDIA driver is easier on regular Mint etc. not only that but LMDE6 only comes with Cinnamon which is a shame as they should offer the option for Xfce. I realize playing around with it in a VM one can install Xfce on it but it would be nice to have a more official Xfce release like how the regular Mint does. so while I heard Debian is a little snappier than Ubuntu (so better on some level), Ubuntu is probably the overall wiser choice unless people don't mind using a bit older stuff etc. but honestly, with some tweaks I would not mind if they changed the main Mint to Debian base instead of Ubuntu. but that's probably not likely to happen unless Ubuntu really does something the Mint team does not like. p.s. but I was building Super Mario 64 (native PC port on Linux) a while ago and it does not like regular Mint but works fine on LMDE6. but at that point I just transferred the final binary out of the VM back to my regular Mint and it works fine.
    • Excel is getting a highly requested PivotTable feature by Usama Jawad Microsoft Excel is one of the most popular software out there, both in the enterprise and personal space. It has a variety of use-cases including data analysis, data crunching, visualizations, and even planning and organization assistance. Microsoft regularly updates Excel with new features, and now, it is introducing a notable feature for PivotTables. Prior to today, PivotTables required a manual refresh from the user whenever new data was inserted into them. This wasn't a particularly complicated process, but it was tedious, so based on significant user feedback, Microsoft has decided to implement auto-refresh capabilities in PivotTables. The good thing is that Auto Refresh is enabled for all new PivotTables by default, but you can choose to disable it by selecting a PivotTable, navigating to the PivotTable Analyze tab, and then clicking on Auto Refresh. It is important to note that Auto Refresh is applicable on a per data source level, which means that the feature's state (on or off) will apply to all PivotTables derived from that source. Additionally, if Auto Refresh is disabled or a PivotTable is unable to synchronize, a message at the bottom of your workbook will say "PivotTable Refresh Needed". Once you click on it, all outdated PivotTables will refresh. Finally, you should keep in mind that external and asynchronous data sources do not support Auto Refresh and that the feature may become unavailable when a co-author is using an older version of Excel or if you are playing around with volatile functions like RAND() and NOW() in your data source. Auto Refresh for PivotTables is available right now in the Beta Channel for Excel for Windows version 2506 (Build 19008.2000) or later, and Excel for Mac version 16.99 (Build 250616106) or later. If you don't see it yet, it's better to wait as new capabilities are often rolled out in a staggered manner.
    • Hell of a legacy, hell of a bad person...
    • Man, what a legend. I know a lot of you don't like social media but the outpouring on the platforms has been really something else.
    • No download links, please.
  • Recent Achievements

    • First Post
      Electronic Person earned a badge
      First Post
    • Week One Done
      CyberCeps666 earned a badge
      Week One Done
    • Very Popular
      d4l3d earned a badge
      Very Popular
    • Dedicated
      Stephen Leibowitz earned a badge
      Dedicated
    • Dedicated
      Snake Doc earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      628
    2. 2
      ATLien_0
      240
    3. 3
      Xenon
      163
    4. 4
      neufuse
      126
    5. 5
      +FloatingFatMan
      124
  • Tell a friend

    Love Neowin? Tell a friend!