Jump to content

This topic is 8248 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

Hey everyone,

I've worked with Javascript previously to calculate simple things like numbers in a CDML fields in a layout with multiple CDML fields to produce a total like this:

<script>

eng1=[fmp-field:grandtotaleng]+0;

document.write("&nbsp;" + eng1);

</script>

The "+ 0" is in case the field is blank and so Javascript knows to just stick a zero in there by default, otherwise it will not calculate.

Now what I would like to do is the comparison of the time a CDML field has and the time it is now (in JavaScript) . Being a minimalist, I know the formula would be ([fmp-field:begin_session]-timeitisnow).

Here is what I have so far:

<script>

//calculating the date it is now....

var today=new Date();

var hours=today.getHours();

var minutes=today.getMinutes();

var seconds=today.getSeconds();

//

//Getting the FMP date from the datefield.....

var time_in=new Date();

var fmp_begintime=[fmp-field:begin_session];

var fmp_hours=time_in.getHours();

var fmp_minutes=time_in.getMinutes();

var fmp_seconds=time_in.getSeconds();

var fmp_time= fmp_hours + ":" + fmp_minutes + ":" + fmp_seconds;

//telling me what time it is now and converting the default 24 hour clock to 12 hours and putting a 0 in front of all seconds and minutes less than 10....

//

tell_time();

function tell_time()

{

if(hours>=13)

{

var hours_converted=hours-12;

}

if(hours<=12)

{

var hours_converted=24-(24-hours);

}

if(minutes<10)

{

minutes="0" + minutes;

}

if(seconds<10)

{

minutes="0" + seconds;

}

//

var time=hours_converted + ":" + minutes + ":" + seconds;

var elapsed_time=fmp_hours-hours_converted;

document.write(elapsed_time);

}

</script>

Now when I run this script just to compare the hours, I get a "NaN" (Not a Number) result. What am I doing wrong?? Is the [FMP-Field:begin_session] supposed to be in the "new Date()" oblect?? Am I supposed to set the [fmp-field:begin_session] to add "+0" to the hours, minutes and seconds as in the first example above?? Thanks for your help!

Posted

Just to keep everyone up to speed as to my further attempts to solve this problem, I will get FM to break down for me the hours, minutes and seconds of the [fmp-field:Begin_session] and let the Javascript calculate them seperately and stick them together at the end as a string. I'll post my further findings here for knowledge on this fuzzy subject. Afterall, G.I. Joe once said "and knowing is half the battle".

Posted

Here are some JS tools you may want to use. The Date.parse() converts a date string to milliseconds from 1/1/1970. You can subtract these numbers and convert back to hours, minutes and seconds.

<script>

var now = new Date() ;

nNowMS = Date.parse(now) ;

document.write(nNowMS + "<br>") ;

var fmtime = "[FMP-Field:time_test]" ;

document.write(fmtime + "<br>") ;

var fmdate = "[FMP-Field:last_app]" ;

document.write(fmdate + "<br>") ;

fmdatearr = fmdate.split("/") ;

document.write(fmdatearr[0] + "<br>") ;

document.write(fmdatearr[1] + "<br>") ;

document.write(fmdatearr[2] + "<br>") ;

dSession = new Date(fmdatearr[2],fmdatearr[1]-1,fmdatearr[0]) ;

document.write(dSession + "<br>") ;

fmtimearr = fmtime.split(":") ;

document.write(fmtimearr[0] + "<br>") ;

document.write(fmtimearr[1] + "<br>") ;

dSession = new Date(fmdatearr[2],fmdatearr[1]-1,fmdatearr[0],fmtimearr[0],fmtimearr[1]) ;

document.write(dSession + "<br>") ;

nSessMS = Date.parse(dSession) ;

document.write(nSessMS + "<br>") ;

</script>


All the best.

Garry

This topic is 8248 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.