- 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-17-2023 09:25 AM
Good morning @Ratnakar7
Thank you for helping. The Date and Time values are coming from a dropdown and it is formatted as 12 Hours.
How to use script to convert 23 HRS into 12 HRS Date/Time format as an input so that the output come out correctly?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 10:08 AM
Hi @Radhika11,
I have tried in background scripts and working as expected.
var date = "2023-07-31 07:00:03";
// Monday, July 31. 2023 7:00 PM
var today = new GlideDateTime(date);
var day = today.getDayOfMonthUTC();
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var dayofWeek = today.getDayOfWeekLocalTime();
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;
template.print("date: " + requiredDate);
Output:
*** Script: Monday, July 31. 2023 07:00:03 AM
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-17-2023 10:22 AM
Hello @Sagar Pagar
We are very much appreciate your assistance. We followed you suggestion and used the code provided. However, we are encountering the following error in background scripts and we don't how to fix it: Please help. Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 10:36 AM
Hi @Radhika11,
I have provided script that will be used in Email scripts.
Note: template.print() will work in Email scripts not in background scripts.
For background scripts try gs.info();
var date = "2023-07-31 07:00:03";
// Monday, July 31. 2023 7:00 PM
var today = new GlideDateTime(date);
var day = today.getDayOfMonthUTC();
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var dayofWeek = today.getDayOfWeekLocalTime();
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);
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-17-2023 11:26 AM
Hi @Sagar Pagar
Thank you for explaining how the code works. I learned something new today.
I have tested and the majority dates and times are working as expected. However, when passed in var date = "12-31-2023 11:03:19 PM", I'm getting undefined for date of the week. Just wanted to know the reason behind it and this is fixable?
*** Script: undefined, December 31. 2023 11:03:19 AM