- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 09:41 PM
Hello,
I need help. I tried to get the date/time data from a field in Change Request. I tried to use getDisplayValue before but the result is the date/time format is not stable. Sometimes got
1. MM/DD/YYYY HH:MM:SS or
2. MM.DD.YYYY HH:MM:SS or
3. MM-DD-YYYY HH:MM:SS
I assumed the unstable of DisplayValue because we received some record from POST API, they may use another format, so that cause of problem.
So, I switch using getValue instead but the format is not satisfied for user YYYY-MM-DD HH:MM:SS, so I tried to find out the solution to convert the date/time format from getValue (YYYY-MM-DD HH:MM:SS) to MM/DD/YYYY HH:MM:SS.
Sample Script:
var gr = new GlideRecord('change_request');
gr.addQuery('number', 'CHG0058582');
gr.query();
if (gr.next())
{
gs.print(gr.start_date.getValue());
}
Result:
2023-05-04 23:00:00
What I need result is 05/04/2023 23:00:00 (MM/DD/YYYY HH:MM:SS format).
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 10:22 PM - edited 07-21-2023 10:30 PM
Hello@Bird1,
Try this updated scripts. It will gives you date time in required format.
var chgRecord = new GlideRecord('change_request');
chgRecord.addQuery('number', 'CHG0058582');
chgRecord.query();
if (chgRecord.next()) {
var start_date = chgRecord.start_date.getValue();
gs.info("start_date: " + start_date);
var today = new GlideDateTime(start_date);
var day = today.getDayOfMonthUTC();
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var time = today.getTime();
var timeCreated = time.getByFormat('HH:mm:ss');
// Set the date format
var requiredDate = day + '/' + month + '/' + year + " " + timeCreated;
gs.info('requiredDate: ' + requiredDate);
}
If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 10:13 PM - edited 07-21-2023 11:45 PM
Hello @Bird1
var gr = new GlideRecord('change_request');
gr.addQuery('number', 'CHG0058582');
gr.query();
var date;
if (gr.next())
{
date=gr.start_date.getValue();
date=date.getByFormat("MM/dd/yyyy HH:mm:ss");
gs.print(date);
}
Plz use this Script for your expected output.
Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.
Regards,
Samaksh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 10:22 PM - edited 07-21-2023 10:30 PM
Hello@Bird1,
Try this updated scripts. It will gives you date time in required format.
var chgRecord = new GlideRecord('change_request');
chgRecord.addQuery('number', 'CHG0058582');
chgRecord.query();
if (chgRecord.next()) {
var start_date = chgRecord.start_date.getValue();
gs.info("start_date: " + start_date);
var today = new GlideDateTime(start_date);
var day = today.getDayOfMonthUTC();
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var time = today.getTime();
var timeCreated = time.getByFormat('HH:mm:ss');
// Set the date format
var requiredDate = day + '/' + month + '/' + year + " " + timeCreated;
gs.info('requiredDate: ' + requiredDate);
}
If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 10:29 PM - edited 07-21-2023 10:31 PM
Hi @Bird1,
No need to split the time and concatenate again.
This will also work.
var requiredDate = day + '/' + month + '/' + year + " " + timeCreated;
removed the unwanted lines and updated with correct.
var Timesplit = timeCreated.split(":");
var strTime = Timesplit[0] + ":" + Timesplit[1] + ":" + Timesplit[2];
If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 11:33 PM
Thank you very much!