cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar

Using Date and Time Functions

Started ‎11-08-2022 by
Modified ‎11-08-2022 by
View Fullscreen Exit Fullscreen

So next, let's talk a little bit about date and time functions. I'm going to clear the contents of the Script Editor window. I'd like to clear the log as well. And I do that by right clicking in the Log window and selecting Clear Log. So remember that dates and times are stored as the number of seconds since midnight January And you can store those in a variable. And we can also use date and time functions to compute date and time values. So I'm going to create a variable t. And I'm going to assign to it, by typing the equal sign, the result of the function, today. And again, I have to add the parentheses for JMP to recognize that that is the JSL function, today. This is going to return today's date and time as the number of seconds since midnight January when I run the script. So let me arrow over the closing parenthesis, type my semicolon-- again, not required, but a best practice. And I'm going to run that line of the script. And we see in the log the number of seconds since midnight January at the moment when I ran my code. Now, it's really difficult for me to interpret this number. It's a large number. For me, I could look at this. I don't know if that's last week. I don't even know if that's last year or next year. So what we can do in our script is we can format that as a character string that is readable. Now, I want to be clear that when you have dates in a data table that are being stored as the number of seconds, we can format them to display as a readable date. But we're not changing the data type or modeling type. They're still being stored as the numeric number of seconds in the data table. We're just displaying them differently. Whereas when I do this with a script, I really am just getting a character string back that cannot be used as in any kind of numeric computations. So there are a variety of formats. I'm going to actually-- instead of clearing the Script Editor, just so you can see the different steps here, I'm going to just go to the next line and type format. And I'm going to open parentheses. I want to format the value stored in t. So that's the first argument. And the format I'll use-- these are all just named, so they're character strings. The format I'll use is called Date Long. So I open my double quotes. JMP automatically closes those. And I type the words Date Long, and I close the double quotes. Now, if I want to run just this one line of the script, one way to do that is to take my cursor over into the margin where the row numbers are and just select that line. And then I can do the Ctrl+R or any of the other methods to run the script. So I'll go ahead and do that. I'll show you another way to run just one line in just a moment. And you can see what's returned to the log is the character string, Thursday, December And I could have stored that result in a variable if I wanted to put that together into a character string that was going to be a message to a user or something that's going to appear in a report. Now, let's continue using the numeric value stored in t. I'm going to go to a new line in my Script Editor window. And I'm going to use a function to calculate a duration, a difference between dates. That's called date difference. And notice, I'm not capitalizing these built-in functions. I generally just leave that up to the reformatting. But I'm not reformatting these, so they're staying all lowercase. But that is not going to affect the parser. I'm going to open my parentheses. And the old date that I want to get the difference from today's date is May. So I'm going to type this out as the little constant, the numbers capital M, a-y, the one month we don't have to abbreviate. But that's always going to be three letters for the month when you are entering a date this way. Notice right now, JMP doesn't recognize what I'm typing. So it's changed the characters there to red. And then I'll type for the year. Now JMP recognizes it as a date and has turned it this dark green color. So that's the old date. I'll put a comma in. The second argument is the date that-- the newer date that you want to get the difference from. Well, we'll just use t. And then you specify the increment. And that's a character string. And I'm going to specify year. And in fact, why don't I go ahead and reformat just to get JMP to capitalize all of those built-in functions for me. And I think it added some spaces maybe that I perhaps didn't. I want to just run line right now. And to run this one line, I mentioned before, you can just select the line. Or you can select several lines. Highlight those to run. But if I only need to run a single line and my cursor is on that line, then I can use the Enter key from the numeric keypad on a standard desktop computer keyboard. The numeric keypad on the right side of that, if I press the Enter key, that runs just that one line. You can see that in the log, that it didn't run the entire script. And that'll work too if I had multiple lines selected. It would only run those selected lines. And so you can see the difference then is years. You may not recognize this date. It's the birthday of Florence Nightingale, who most of us know as a very famous nurse. She actually also, though, was a pioneering epidemiological statistician. And she invented some of the graphs that are still being used by epidemiologists today. So we include her in our course a couple of places just to recognize her for those contributions. So that's all for now for dates and times. Again, we're taking a brief tour through some of the basic elements of JMP.