- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2023 11:27 PM
Hello,
Could some one please help to format date and time in email script?
Current Print format: 2023-07-31 07:00 03
Would like to change to this format: Monday, July 31. 2023 7:00 PM
Here is the email script:
Thank you
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2023 09:34 AM
Hi @Radhika11,
For workaround you can try the below scripts,
// var date = "2023-10-31 07:00:03";
// Monday, July 31. 2023 7:00 PM
//
var date = "2023-07-31 11:03:19 PM";
if (new GlideDateTime(date).isValid()) {
var today = new GlideDateTime(date);
gs.info(today);
var day = today.getDayOfMonthUTC();
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var dayofWeek = today.getDayOfWeekLocalTime() % 7;
var time = today.getTime();
var timeCreated = time.getByFormat('HH:mm:ss a');
var Timesplit = timeCreated.split(":");
var strTime = Timesplit[0] + ":" + Timesplit[1] + ":" + Timesplit[2];
// Get the name of the month
var monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var monthName = monthList[month - 1];
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dayName = weekday[dayofWeek];
// Set the date for the notification
var requiredDate = dayName + ', ' + monthName + ' ' + day + '. ' + year + ' ' + strTime;
gs.info(requiredDate);
} else {
gs.info("not valid date");
}
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-13-2023 01:27 AM
Hi @Radhika11 ,
To format the date and time in the desired format, you can use the GlideDateTime API in ServiceNow. Here's an example of how you can modify your email script to achieve the desired format:
var startDate = current.variables.select_start_date_and_time;
var formattedStartDate = new GlideDateTime(startDate).getDayOfWeekName() + ', ' +
new GlideDateTime(startDate).getMonthName() + ' ' +
new GlideDateTime(startDate).getDayOfMonth() + ', ' +
new GlideDateTime(startDate).getYear() + ' ' +
new GlideDateTime(startDate).getHourOfDayLocalTime() + ':' +
new GlideDateTime(startDate).getMinute() + ' ' +
new GlideDateTime(startDate).getDayPart();
template.print('<span style="color:#2f5597">' + formattedStartDate);
If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the ✅Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.
Thank you!
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2023 08:36 AM
Good morning @Ratnakar7
Thank you so much for helping. I used your suggestion code, but I'm getting the following error:
Start: undefined, undefined 13, 2023 undefined:undefined undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2023 06:19 PM
Hello,
Please help on date and time format in email script.
The input time is "PM"
The output as 'AM" . I don't know where I screwed up.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 05:49 AM
Hi @Radhika11 ,
The GlideDateTime API accepts the 24hrs date/time format as input:
try below code:
var gDate = new GlideDateTime('07-13-2023 017:55:48').getDate();
var gTime = new GlideDateTime('07-13-2023 017:55:48').getTime();
var startdate = (gDate.getByFormat(' EEEE dd MMMMM') + gTime.getByFormat( 'hh:mm:ss a'));
gs.print(' Start: '+startdate);
Thanks,
Ratnakar