[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript Server date(); Not new date();

Posted on 2014-04-17
9
Medium Priority
?
454 Views
Last Modified: 2014-04-25
I want to use server date not the new date(); in this script

var oneHour = 60 * 60, // 60 seconds * 60 minutes dynamic variable
    twentyMins = 20 * 60, // 20 minutes * 60 seconds
    thirtyMins = 30 * 60, // 20 minutes * 60 seconds
    oneMin = 60, // 60 seconds
    fiveSecs = 5,
    when = new Date(2014,2,18,8,32,0), // change to the date and time you want to show - months start at 0
    timeToShow=thirtyMins
    link = '<input type="button" value="Get free MCLE Credit" name="MCLE" onclick=\'window.open("index.cfm","_self")\' />';


window.onload = function () {
    var then = new Date(when.getTime());
    then.setMinutes(then.getMinutes()+30);
    var d = new Date();
    if (d.getTime() >= when.getTime() && d.getTime() <then.getTime()) {
      document.getElementById('btnappear').innerHTML=link;
      countDown('btnappear', '', timeToShow);
    }
}

function countDown(elID, output, seconds) {
    var mins,
    secs = seconds,
        pad = function (n) {
            return n > 9 ? n : '0' + n;
        };

    // get the hours by dividing by the number of seconds in an hour
    hours = Math.floor(secs / 3600); //  60 * 60 = 3600
    // get the remaining seconds
    secs %= 3600;
    // likewise, get the number of minutes of by dividing the remaining seconds by 60
    mins = Math.floor(secs / 60);
    // again, get the remainder of seconds
    secs %= 60;

    // pad any numbers less than 9 with a leading 0
    secs = pad(secs);
    mins = pad(mins);
    hours = pad(hours);

    document.getElementById(elID).innerHTML = (seconds === 0) ? output : 'Time link is still active: ' + hours + ':' + mins + ':' + secs +'<br/>'+link;

    // instead of returning, just don't call setTimout if we are done
    if (seconds !== 0) {
        seconds -= 1;
        // there is no need to pass a string to setTimout, you just pass the function name,
        // followed by the timeout in ms, followed by any params
        //setTimeout(countDown, 1000, elID, output, seconds);
        setTimeout("countDown('" + elID + "', '" + output + "', " + (seconds) + ")", 1000);
    }
}
0
Comment
Question by:smares323
  • 4
  • 3
8 Comments
 
LVL 34

Expert Comment

by:Big Monty
ID: 40007448
javascript runs on the client side, so will return the date of the client.

To get the date of the server, you would most likely want to use a server side language such as asp.net, php, etc.
0
 

Author Comment

by:smares323
ID: 40007468
That's true client side I noticed that when I was testing the clients clock was set to a different time and the button did not appear on time that it was set. Can I some how use a Server date time I'm running ColdFusion.
0
 
LVL 34

Expert Comment

by:Big Monty
ID: 40007478
change

var d = new Date();

to

var d = new date('<cfoutput>#Now()#</cfoutput>');
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:smares323
ID: 40007677
#Now()# is outputting this format {ts '2014-04-17 13:15:37'}  and javascript is throwing out an error.
0
 
LVL 34

Expert Comment

by:Big Monty
ID: 40007689
im not a cold fusion guy, but doing a quick search looks like this should work (assuming you just want the date):

var d = new date( '<cfoutput>#dateformat(now(),"dddd, mmmm dd, yyyy")#</cfoutput>' );
0
 

Author Comment

by:smares323
ID: 40007969
I wrote the following code bellow but the script does not like the value from var d, even do it's formatted exactly like the new date(); value. the button does not pop up. only pops up with new date(); My problem is that someone my have the wrong time set on there computer and never see the pop up button.

var cd = "#dateformat(now(),'mm/dd/yyyy')# #TimeFormat(now(),'HH:mm:ss')#";
var jd = cd.split(/\D/);
var d = new Date( jd[2], jd[0]*1-1, jd[1], jd[3], jd[4], jd[5] );
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 40008168
Javascript's Date should accept milliseconds, which you can get from:

       #now().getTime()#

So try:

<script>
   <cfoutput>
     var d = new Date( #now().getTime()# );
    </cfoutput>

    // for debugging purposes
    alert( d );
</script>
0
 

Author Closing Comment

by:smares323
ID: 40023842
great advice
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question