The CreatorCon Call for Content is officially open! Get started here.

Need help on remove Seconds from Date/time field.

Jessica28
Tera Guru

Hello,

We have a Start Date/Time variable on a Catalog Item like this:

Jessica28_1-1694636506890.png

 

Below screenshot was from a email script.  How to remove seconds from time?

 

Jessica28_0-1694636361058.png

 

Thank you

1 ACCEPTED SOLUTION

 

var strTime = "0:4:00:00 PM";
var spacePos = strTime.indexOf(" ");
var firstPart = strTime.substring(0, spacePos-3);
var lastPart = strTime.substring(spacePos);
gs.info('spacePos = ' + spacePos + ', firstPart = ' + firstPart + ', lastPart = ' + lastPart);
strTime = firstPart + lastPart;
gs.info('strTime = ' + strTime);

 

You can use logic in lines 2 -6 without the 'gs.info()' line. Parse on the space since hours and minutes can be single digits. the above results in " strTime = 0:4:00 PM".

View solution in original post

10 REPLIES 10

Bert_c1
Kilo Patron

Hi jessica28,

 

I found this link when viewing your post:

 

https://www.servicenow.com/community/developer-forum/how-to-set-seconds-to-00-and-round-to-quarter-h...

 

We the author states: "I've also set it up to use an onLoad script to store my functions then simply call those functions from an onChange script tied to my start time and duration fields."

 

You can the 'roundStartTime()' function there for your "Start / Date Time" variable in a Catalog Client Script it seems.

 

But then, this won't help with the display of the value on the form if you don't want seconds shown.

Thank you for sharing your finding @Bert_c1 

Let me give it a try and provide feedback. 

Hi @Bert_c1 

I have tried the suggestion codes and it rounds seconds to 00 and minutes to the nearest quarter hour.

However, it is still showing the seconds on the email notification. Is it possible to remove the seconds completely from the output. I'm very weak at JavaScript to be able to modify the code to remove it.  Can you or someone help?   Thank you

 

Jessica28_0-1694663170045.png

 

Hi @Jessica28,

 

I tried a test in my instance, on a DateTime field, but didn't get it to work with just one function in the 'onLoad' client script to set the seconds to '00' and calling that function in an 'onChange' client script. Seems you were successful getting the example to work, but you didn't post either script. So I can't advise on how to just set the seconds to "00" with out the rounding.

 

Now, on eliminating the ":00" seconds from the form value, I can't help with that.

 

 And changing the Time Format system property mentioned below will affect all DateTime display values in your instance. I believe you only want seconds removed for the one field on the form. Or is it just what is displayed in an "email notification". If so, I believe other Community members can help with that.