0

Okay, I'm completely new to this kind of coding; I've had some experience in SQL & VB.net. I'm wondering if the following would be possible. Whatever code we come up with has to be able to work in an i-frame (as javascript of HTML5).

I need a text link to appear if the time is after 9:00pm on 04th of March 2014 GMT+ 0:00. Or alternatively I could re-direct the person to the desired page if the time is after that time.

How can this be done? I don't have access to the root servers as my site's hosted by a webmaster (wix.com) so I'd have to obtain the current time from somewhere -- like google perhaps.

Is this possible?

{Edit 1}

I've tried this code that I've changed a little from W3 Schools, you can copy and paste min, click submit and then run it from here; Link

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get a time-based greeting.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x="";
var time=new Date().getHours();
if (time<21)
  {
  x="Good morning";
  }
else
  {
  x="Good evening";
  }
document.getElementById("demo").innerHTML=x;
}
</script>

</body>
</html>

The code works fine, I just need to put the date in, any ideas on how to do that?

user3224987
  • 83
  • 3
  • 12
  • Can't you use javascript `new Date()` to get the time? – Joqus Mar 03 '14 at 18:49
  • Don't use javascript for this as the users might hack the time on their pc and get the link anyways... If you want to do it right, use PHP – Daniel Bejan Mar 03 '14 at 18:50
  • If you want to use the time from the user system, you can use `new Date()` provided by JavaScript. Otherwise, if the page is dynamic, you could use a back end language like C# to get the server's time. – Chirag Bhatia - chirag64 Mar 03 '14 at 18:52
  • @csanonymus I'm unable to use PHP. I know, bumber. It would need to be javascript or HTML, It's nothing major so it doesn't matter if someone hacks their time. – user3224987 Mar 03 '14 at 18:52
  • http://stackoverflow.com/questions/9756120/utc-timestamp-in-javascript Have a look there – Daniel Bejan Mar 03 '14 at 18:54
  • 1
    `if(new Date(Date.UTC(2014, 02, 4, 21, 00, 00))) < new Date())` will work – Joqus Mar 03 '14 at 18:54
  • JavaScript doesn't allow you to fetch data from other sites unless your server explicitly allows cross site HTTP requests in the http headers – Chirag Bhatia - chirag64 Mar 03 '14 at 18:56
  • Thanks for your help so far, please see my latest edit. – user3224987 Mar 03 '14 at 19:08
  • 1
    Have you looked at my answer? – Joqus Mar 03 '14 at 19:36
  • Yes @Joqus, I'm unsure on how to implement it into a run-able code. I'm very new to this kind of codeing. – user3224987 Mar 03 '14 at 19:40
  • 1
    Ok, maybe you can create a [jsfiddle](http://jsfiddle.net/) and write something, then I can suggest some improvements. – Joqus Mar 03 '14 at 19:48
  • @Joqus Thanks for your suggestion, I'm very unfamiliar with that site. I've added my code so far as an answer to this thread. – user3224987 Mar 03 '14 at 19:56
  • 1
    @user3224987 Ok, check this out http://jsfiddle.net/Joqus/pE9hH/1/ Be aware that I am using jQuery but you can use what you want to hide the element. It is also dependent on the user time, but if is not a problem for you... – Joqus Mar 03 '14 at 20:02
  • Thank you very much @Joqus your answer helped me, I was formatting the date/time wrong :) Thank you Very much! I can work the rest out now. Great help! – user3224987 Mar 03 '14 at 20:09
  • It will be nice if you could accept my answer so the question is finished here. – Joqus Mar 03 '14 at 20:15

4 Answers4

1

If you can use the browser current date you can do a simple check:

    if(new Date(Date.UTC(2014, 02, 4, 21, 00, 00))) < new Date()) { 
        $('#youlink').show();
    }

Assuming that you are using jQuery.

I you want to the link appear while the user has the page open, you can set a timer to run on that date:

var timeInterval = new Date(Date.UTC(2014, 02, 4, 21, 00, 00)).getTime() - 
(new Date()).getTime();
    setTimeout(function () { // Show the link
 }, timeInterval):
Joqus
  • 585
  • 5
  • 19
0

i understand that you can't use local time in you host server ? strange but then you have to use a webservice here the code how to use webservices in javascript and you must find a webservice wsdl link in the internet and use it

<html>
  <head>
   <meta http-equiv="refresh" content="2" />
   <title>Get Date Time</title>
   <script language="JavaScript">
    var iCallID;
    function InitializeService(){
     service.useService(http://exemple.com/MyWebService.asmx?wsdl, 
    "GetDateTimeService");
     service.GetDateTimeService.callService("GetDateTime");
    }
    function ShowResult(){
     alert(event.result.value);
    }
   </script>
  </head>
  <body onload="InitializeService()" id="service" 
    style="behavior:url(webservice.htc)" onresult="ShowResult()">
  </body>
 </html>
Alaeddine
  • 1,571
  • 2
  • 22
  • 46
0

If your web server has server-side includes enabled, you can use conditional SSI directives to decide whether to include the link or not. This will allow you to use the server time without relying on PHP or Javascript.

For example:

<!--#config timefmt="%y-%m-%d %H" -->
<!--#if expr="$DATE_LOCAL >= /2014-03-04/" -->
<a href="#">Link</a>
<!--#else-->
<p>Coming soon!</p>
<!--#endif --> 

You might need to do some nesting to get what you want, since expression evaluation is pretty basic and uses string comparison only.

Take a look at these links for more information:

http://httpd.apache.org/docs/current/howto/ssi.html

http://www.ssi-developer.net/ssi/conditional-expressions.shtml

http://www.htmlgoodies.com/beyond/webmaster/article.php/3473351/SSI-Dates-and-Times.htm

nullability
  • 10,545
  • 3
  • 45
  • 63
0

My code so far:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get a time-based greeting.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x="";
var time=new Date().getHours();
var date=new Date().getDate();
if (date>5) <---- I'd like to add the exact time here, I was thinking about a nested if.
  {
  x="GOOD";
  }
else
  {
  x="BAD";
  }
document.getElementById("demo").innerHTML=x;
}
</script>

</body>
</html>

As you can see I was planning on doing a nested if so it checks the date and then the time. But I'd like to do it all at once...

user3224987
  • 83
  • 3
  • 12