Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Choose Language Hide Translation Bar
Highlighted
msharp
Super User

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?

8 REPLIES 8
Highlighted
ih
ih
Level VII

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.

 

Highlighted
msharp
Super User

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.

Highlighted
msharp
Super User

Re: JMP Time Formatting

Highlighted
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.

Highlighted

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.

Learn it once, use it forever!
Highlighted
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

Highlighted
ih
ih
Level VII

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
Highlighted
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.

Article Labels

    There are no labels assigned to this post.