Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Extract just time from the time_worked field

MBarrott
Mega Sage

I'm passing over the value from the time_worked field via g_scratchpad but noticed it is also passing over a placeholder date (e.g 1970-01-01 01:00:00).

 

g_scratchpad.savedTime = current.time_worked;

 

Is there a easy way to simply remove the date so that the passed over value is just "01:00:00"? 

1 ACCEPTED SOLUTION

sejal1107
Tera Guru

Hi @MBarrott 

g_scratchpad.savedTime = current.time_worked.toString().split(" ")[1]; 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers

Thanks,

Sejal

Please check and Mark Helpful and Correct if it really helped you.
Thanks & Regards
Sejal Chavan

View solution in original post

6 REPLIES 6

Robbie
Kilo Patron

Hi @MBarrott,

 

When dealing with date and time fields, don't forget timezones also need to be considered.

 

Rather than reinvent the wheel, here's a great article from @tiagomacul who provides both context and examples - Kudos.

Take a look - https://www.servicenow.com/community/developer-articles/scripts-how-to-get-only-hour-on-date-time-fi...

 

However, just in case links break in the future - here's a sample of the script the article provides when using a sample date "2021-03-08 16:24:06":

function timenow()
{
  var gdttime = new GlideDateTime(current.time_worked); 
  var time = gdttime.getTime();
  var timeCreated = time.getByFormat('HH:mm:ss');
  
  return timeCreated;
}

gs.log("UDT time now  = " + timenow());

Sample result "16:24:06"

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

Thanks for sharing