BookmarkSubscribeRSS Feed
msharp

Super User

Joined:

Jul 28, 2015

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?

3 REPLIES
ih

Community Trekker

Joined:

Sep 30, 2016

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

Joined:

Jul 28, 2015

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

Joined:

Jul 28, 2015

Re: JMP Time Formatting