Multiplying Time fields

Hi

I am trying to use a time field in a calculation by multiplying it by a value in a number field

eg. 1:30 minutes x 4 = 6:00 Hours
time_field * number_field = c_time_field

Currently this calculation creates some very odd time values even though i am formatting the field hh:mm. i assume i have to force a format somewhere in the calculation but i am unsure which function to use

Would appreciate any help.

I assume your time field is based on a datetime dataprovider. Right?

If so this:

ven though i am formatting the field hh:mm

doesn’t help. Formatting the field doesn’t change the value but only the display mode of the value.

What you should do is extract the time portion and multiply it.

If not, 1:30 will never be seen as 1.5 which it actually is in the ‘decimal’ world of adding, substracting etc.

Hope this helps.

Thanks Marcel

Yes, it is a Datetime dataprovider

I do convert this to a decimal and multiply this, and it works fine. But i still need the user to see it as time multiplied as well.

How do i extract just the time data out of the datetime field?

Have a look in the methodeditor Navigator tree:

JSLib:Date:get/set Hour() , Minutes() etc

Hi

Thanks again,

i’m playing around with the get/set fx now. wondering would it be easier, since i already have the multiplied value as a decimal to convert this to a time?

eg 15.75 becomes 15:45 (HH:mm)

Is this an easier conversion and how do i convert it?

Why not split the values and multiply the minutes bit. In this case .75 * 60…

Still have issues inserting this value into the datetime field. Getting a result of 10:00 always no matter what goes into the field!

I am unsure why 10:00 but my time difference to GMT is 10:00 Hours.

Anyone converted numbers into a datetime (HH:mm) field eg. 18.50 to 18:30?

or mutliplying a datetime (HH:mm) by a number into another datetime field (HH:mm) eg. 1:15 x 3 = 3:45

Can you show us the part of the method where you extract and insert the new values?

Hi Marcel

Here is my current method, not sure if i am on the right track

// extract left 2 numbers and right 2 numbers (not sure if this is the best way to do it?)
var dec_upper = utils.stringLeft(inventory_to_build_labour_lines.time_total_dec_build, 2);
var dec_lower = utils.stringRight(inventory_to_build_labour_lines.time_total_dec_build, 2);

// convert dec to minutes
var time_lower = dec_lower * .6;

// create the full converted time
var time_build = dec_upper + “:” + time_lower;

// insert into datetime field (its here i believe the error exists)
time_total_build = time_build;

I would prefer not to do this by method but as a calculation if possible.

Huh, you told me that you were working with datetimes.

You are performing string operations on them in your examples.

That will NEVER work as expected. Use the JavaScript .date() functions (see methodeditor/navigator).

There is no issue in doing that in a calculation but again, string operations on datatimes will NOT do it…

Hi

Again this problem can be solved 2 ways:

  1. Multiplying a datetime field by a number field = calculated datetime
  2. Converting a number field (this has already been multiplied and formatted 00.00) into a datetime field

Whichever way is the easiest! This is the first thing to determine

The previous post referred to the 2. above and therefore doesn’t involve datetimes until the end.

if i try by 1. above i can use the following in a method:

// extract hours and minutes to create separate variables
var hours = time_total.getHours();
var minutes = time_total.getMinutes();

// multiply the variables by the number field
var time_build_hours = hours * kit_multiplier;
var time_build_minutes = minutes * kit_multiplier;

fine, all works well what i don’t know is how to insert these values back into a datetime field using the setHours/setMinutes fx? i keep getting errors and there is no example code to follow in developer.

Also if the variable “time_build_minutes” is greater than 60 will this cause issues inserting back into minutes field?

How do i use the setHours/Minutes function to insert these variables into the datetime field?

appreciate your patience! this seems a trivial thing to do and takes me about 30 secs. in FMP. Have you tried this yourself?

Have you tried this yourself?
```What do you think?

Then i would appreciate Obi-Wan knowing how you did it!

fine, all works well what i don’t know is how to insert these values back into a datetime field using the setHours/setMinutes fx? i keep getting errors and there is no example code to follow in developer.

Can you give me what you are doing here? Should not give any errors.

Also if the variable “time_build_minutes” is greater than 60 will this cause issues inserting back into minutes field?

It will be no issue at all, 70 minutes will add 1 hr and 10 minutes.

How do i use the setHours/Minutes function to insert these variables into the datetime field?

date.setHours(xx) and date.setMinutes(xx) or date.setHours(date.getHours + xx) etc. But again, can you show that part of your method?

GOT IT! WORKS!

Only took me 2 days and twenty posts!

final code for a method

// extract hours and minutes to seperate variables
var hours = time.getHours();
var minutes = time.getMinutes();

// multiply the variables by the number field
var build_hours = hours * build_multiplier;
var build_minutes = minutes * build_multiplier;

// insert into total time field
total_build_time = total_build_time.setHours(build_hours);
total_build_time = total_build_time.setMinutes(build_minutes);

The trick was how you assign in the values at the end which i didn’t have right. I’ll play around changing this so its a calculated field not a method.

Thanks Obi-Wan

You are so welcome! Glad you got it working now :)