- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2024 03:45 AM
Hi Everyone,
I have a table with three fields : Session Start (Date/Time field) , Session End (Date/Time Field) and Session Duration (Floating Point Number).
I have a JSON payload which has Session Start and Session End in Epoch Timestamp. I have to convert them to normal format and populate into fields
.
Payload :
{
"sessionStartTime": 1718002385195,
"endTime": 1718002385188,
},
{
"sessionStartTime": 1716978505922,
"endTime": 1716978507303,
}
Scripted Rest API:
var requestBody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(requestBody);
var SessionStart = parsedData.sessionStartTime;
var SessionEnd = parsedData.endTime;
var gdt1 = new GlideDateTime();
gdt1.setNumericValue(SessionStart);
var gdt2 = new GlideDateTime();
gdt2.setNumericValue(SessionEnd);
var sub = new GlideDateTime.subtract(gdt1, gdt2);
var diff = sub.getDisplayValue();
var gr = new GlideRecord("custom_table_name");
gr.initialize();
gr.setValue("session_start_time", gdt1);
gr.setValue("session_end_time", gdt2);
gr.setValue("session_duration", diff);
gr.insert();
At times the difference between two dates is coming in minutes , sometimes second and sometimes hours. How can I make uniform . Like I should get value in minutes only
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2024 05:58 AM - edited 06-13-2024 06:01 AM
That's how getDisplayValue() method works for GlideDuration objects. If the field you are trying to populate is of type Duration, simply use getValue() instead of getDisplayValue() on your sub variable:
var sub = new GlideDateTime.subtract(gdt1, gdt2);
var diff = sub.getValue();
Otherwise, if you are using a string field, use getByFormat() method to retrieve different pieces of information separately and combine them as needed:
var sub = new GlideDateTime.subtract(gdt1, gdt2);
var diff = sub.getByFormat('HH');
var diff = sub.getByFormat('mm');
There are also other methods for working with GlideDuration objects available. They are documented here.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2024 05:58 AM - edited 06-13-2024 06:01 AM
That's how getDisplayValue() method works for GlideDuration objects. If the field you are trying to populate is of type Duration, simply use getValue() instead of getDisplayValue() on your sub variable:
var sub = new GlideDateTime.subtract(gdt1, gdt2);
var diff = sub.getValue();
Otherwise, if you are using a string field, use getByFormat() method to retrieve different pieces of information separately and combine them as needed:
var sub = new GlideDateTime.subtract(gdt1, gdt2);
var diff = sub.getByFormat('HH');
var diff = sub.getByFormat('mm');
There are also other methods for working with GlideDuration objects available. They are documented here.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/