Free Function Friday Ep.27 – currentTime

March 25th 2016 08:00:50am

Welcome to the Free Function Friday series. This week’s function will get us the current time in a manageable way. Normally when calling the Date() method you have to follow up with the getHours, getMinutes, and the getSeconds methods.

Like so…

var d = Date(); //Assumes time is 4:47:58 PM PST
var h = d.getHours();	//Returns 16
var m = d.getMinutes();	//Returns 47
var s = d.getSeconds();	//Returns 58

How the data returns will mostly make sense. Hours for example though will start at 0 then increment to 24. This is because computers use a 24 hour base. Minutes and seconds will have single digit numbers for 1-9 instead of 01, 02, 03, and so forth. Most digital clocks (in the U.S.) use the format like this, 2:34 PM.

Some computer setups include the seconds as well like this, 2:34:05 PM. What we build in this function will allow us to customize the separator and choose wether or not to include the AM/PM suffix. Why is this a helpful function you ask? I’ve used this very function when exporting data from a project into a text doc and I would use a time suffix for the name of the document to help keep things organized.

So if I was just exporting out a list of say, installed effects during an install cycle, I would be using a file name like “myEffects.txt”, but then add the current time to then prevent myself from accidentally overwriting the file as I exported the doc throughout the install process.

My file names would show as:

  • myEffects_2_34_05.txt
  • myEffects_2_50_29.txt
  • myEffects_3_26_12.txt

Setting the separator to “_” helped in making a string that was compatible with file name structures. Another example is just needing to place the time a process was last run in your script UI for the user to have feedback.

Say they clicked a button in your UI to organize the project, you could have a bit of text that says “Project was last organized at 2:34:05 PM” posted in your UI. Hopefully that gives you a few ideas.

Source Code:

alert(currentTime(false, ":"));

function currentTime(amPm, sep){
	var a, h, m, s, ap, sep;
	a = new Date();
	hCall = a.getHours();
	h = (hCall == 0) ? h=12 : h=a.getHours();
	m = a.getMinutes();
	sCall = a.getSeconds();
	s = (sCall.length == 1) ? "0"+sCall.toString() : sCall;
	if(amPm == true){
		(hCall > 12) ? ap=" pm" : ap=" am";
	}else if(amPm == false){
		ap="";
	}
	return h + sep+ m + sep+ s + ap;
}
Checkout more:
Expression Shorts Trigger Event Via Speed
Houdini Mardini Wind
Houdini FFX Align To Curve
Cinema4D Attach Splines To Alembics
X-Particles Water Bubble Tank