cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
msharp
Super User (Alumni)

JMP Time Formatting

I can't get the time format to work.  According to the scripting guide the format "m/d/y h:m:s" is used for both 12hr and 24hr time... WTF?  Why would I ever want a function to willy nilly give one or the other answer?  How do I get this to work!  

time = today(); //3619617166
formattedTime = format(time, "m/d/y h:m:s");
print(formattedTime);  //"09/12/2018 5:12:46 PM"
//What I want: "09/12/2018 17:12:46"


There's some cryptic notes in the Scripting Guide that make me feel it chooses 12hr or 24hrs based on your computer settings, which is a terrible idea b/c I don't want my code to break on half of my users computers.

If formatting doesn't work, is there a simple work around?

10 REPLIES 10
ih
Super User ih
Super User

Re: JMP Time Formatting

I often end up using this kind of thing:

names default to here(1);
time = 3619617166;
pad = function( {x}, if(x < 10, "0" || char( x ), char( x )));

pad(month(time)) || "/" || pad(day(time))|| "/" || char(year(time)) || " " || 
	pad(hour(time)) || ":" || pad(minute(time)) || ":" || pad(second(time));
// "09/12/2018 17:12:46"

+1 to the solution with a customizable format string.

 

msharp
Super User (Alumni)

Re: JMP Time Formatting

Thanks @ih; that is a decent work around.  One note, believe you need to put hour(time, 24) to force it to 24 hour time.  It appears to default to 24 hours, but the documentation makes me feel like it wants to default to 12 hours.

 

//My update: (just slightly shorter)

format(time, "m/d/y") || " " || pad(hour(time, 24)) || ":" || pad(minute(time)) || ":" || pad(second(time));

I also hope there's a customizable format string solution.  If there isn't a better answer in the next week, I'll add the request to the JMP Wish List.  Hopefully someone from JMP can reply.

msharp
Super User (Alumni)

Re: JMP Time Formatting

Chris_Rodrigues
Level III

Re: JMP Time Formatting

Just went down the rabbit hole of trying to figure out how to make date formats appear the way I want on my charts.  I never expected it to be this hard.  So I have to build it up character by character and roll my own function to pad hours/minutes/seconds with leading zeroes.  Awesome.  Thanks @ih for the tip.

Re: JMP Time Formatting

Please also take a look at Help > JMP Documentation Library > Scripting Guide and search on 'custom functions.' The information starts on page 393 for JMP 15.1 release. Here you will also find custom transformations and custom formats. These definitions can then also be used in the JMP UI interactively.

Chris_Rodrigues
Level III

Re: JMP Time Formatting

On my charts I wanted to use the X axis label space as efficiently as possible so I didn't want things like year, # of seconds, etc. "Mon 18:00" is sufficient.  At first I thought the Format() function would be able to do this easily, but this is what I ended up using:

 

Names Default To Here( 1 );
pad = Function( {x}, If( x < 10, "0" || Char( x ), Char( x ) ) );
Char( Match( Day Of Week( :value ), 1, "Sun", 2, "Mon", 3, "Tue", 4, "Wed", 5, "Thu", 6, "Fri", 7, "Sat" ) ) || " " || pad( Hour( :value, 24 ) ) ||
":" || pad( Minute( :value ) );

and this is the result:

 

jmp graph custom timestamp.jpg

ih
Super User ih
Super User

Re: JMP Time Formatting

Hello @Chris_Rodrigues,

 

You might take a look at the function below and, if you like it, vote for the idea on the JMP Wish List here.  I use something like this all the time and would love to see it made available as a built-in function.

Names default to here(1);
time = 3619617166;
pad = function( {x}, if(x < 10, "0" || char( x ), char( x )));

formatdatetime = function({time, pattern, prepend=""},
	char(substitute(pattern,
		prepend || "H", pad(hour(time, 24)),
		prepend || "h", pad(hour(time, 12)),
		prepend || "M", pad(minute(time)),
		prepend || "S", pad(second(time)),
		prepend || "m", pad(month(time)),
		prepend || "d", pad(day(time)),
		prepend || "y", char(year(time)),
		prepend || "AP", char(if(hour(time)==24, "AM", if(hour(time, 24)>11,"PM","AM"))),
		// the rest of the formatting options..Month name, 
		// short month name, hours without padding, etc
	))
);

formatdatetime(time, "m/d/y h:M:S AP");
// 09/12/2018 05:12:46 PM

formatdatetime(time, "ymd_HMS");
// 20180912_171246

formatdatetime(time, "hour = %h, minute = %M", "%")
// hour = 05, minute = 12
Chris_Rodrigues
Level III

Re: JMP Time Formatting

Thanks. I voted on the wish list item already  I don't particularly like any of the default date formats so I would definitely use this function if it were available as a built-in function.  Now that I know what is required, I'm past the learning curve and I can format my dates however I want.  Limitless flexibility is useful.  But if I tried to explain all this to a non-programmer Excel user I would get blank stares.  Seems excessively complicated when Excel has been able to accept date formats of "ddd hh:mm" since at least 1997.

Newbie_1
Level I

Re: JMP Time Formatting

I'm with you, Chris.  The JMP function is idiotic.  I shouldn't have to write a script just to change a time format from AM/PM to 24h.  Other threads gave formulas for this, none of which worked.  There is ZERO reason this should not be a standard JMP format in the dropdown list. It's been 3 years since your post and still nothing from JMP.