- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2017 10:09 PM
Hello Community,
I am trying to write a simple client script to compare two date variables. What I thought of achieving through a simple script doesn't seem to be simple any more and here is the complete case.
Environment: Helsinki Patch 8
We have a custom application named "Trade Show Management". It has a table named "Trade Shows" where trade shows are pre-created by the trade show admins and employees can register/signup for the tradeshows in "accepting registration" status.
Another table by name "Signups" is created with several columns and one of them is trade_show which is a reference to the "Trade Show" table.
For the users to signup for the tradeshows a record producer catalog item is configured.
The catalog item has the following variables
- Attendee (Reference Field)
- Trade Show (Reference field)
- Trade Show Start Date (should be read only, and should populate based on the start_date of the Trade Show selected)
- Trade Show End Date (should be read only, and should populate based on the end_date of the Trade Show selected)
- Attendee Start Date (Date, read write, user manually selects a date)
- Attendee End Date (Date read write, user manually selects a date)
I am able to populate the "Trade Show Start Date" and "Trade Show End Date" successfully, when the user selects a "Trade Show". I did this with a client script written on "onChange" condition over the "Trade Show" variable.
Here is the problem: the dates are populated in the "YYYY-mm-dd" format.
I've checked the global date format on the system properties and it is set to "mm-dd-YYYY" format and the user date format is also set to "mm-dd-YYYY"
Not sure why it still shows the date in "YYYY-mm-dd" format.
To understand this a more I made these two fields read-write and let the user select/change the dates manually. When a user manually selects the dates its in the desired "mm-dd-YYYY" format.
Is this a bug?
Formatting is not the only issue for me. I need to compare/impose some validations/constraints over these two variables i.e. the "Attendee Start Date" shouldn't be after the "Trade Show End Date" and the "Attendee End Date" shouldn't be before "Trade Show Start Date". I've a client script to validate this and stops the user from submitting if one of these conditions is not met.
I've a similar requirement to check the "Attendee Start Date" and "Attendee End Date" variables i.e."Attendee End Date" shouldn't be before "Attendee Start Date". I am successfully able to impose that constraint through the same script.
The comparison between "Trade Show End Date" and "Attendee Start Date" and "Trade Show Start Date" and "Attendee End Date" also works perfectly when the "Trade Show Start Date" and "Trade Show End Date" are manually selected and the format is in "mm-dd-YYYY" format. So I concluded that it is the formatting of "Trade Show Start Date" and "Trade Show End Date" that is causing the problem.
I am pasting the client script to populate those dates and change their formatting. Please review and advise me where I am going wrong or is there another way to format the dates.
Script # 1 - Populate Show Dates, Client Script type onChange, variable name "u_trade_show"
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == ''){
return;
}
var tradeShow = g_form.getReference('u_trade_show, fetchDate);
function fetchDate(tradeShow) {
if (tradeShow) {
g_form.setValue('u_start_date', tradeShow.start_date); //populates the correct date but in "YYYY-mm-dd" format
g_form.setValue('u_end_date', tradeShow.end_date); //populates the correct date but in "YYYY-mm-dd" format
//tried the following way to change the format
var tssd = getDateFromFormat(tradeShow.start_date, g_user_date_format);
var tsed = getDateFromFormat(tradeShow.end_date, g_user_date_format);
g_form.setValue('u_start_date', tssd); //populates a value of "0"
g_form.setValue('u_end_date', tsed); //populates a value of "0"
}
}
}
Appreciate your time and help!
Thanks
Pa
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2017 12:50 AM
HI Paul,
I belive u need to use date format as below i have posted the example.
var date = new Date('2010-10-11T00:00:00+05:30');
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 07:37 AM
It is now displaying the correct date after I append the timezone i.e. T00:00:00-04:00'
Thanks again Prakash nana
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 07:43 AM
Hi Paul,
If you got your result.Please mark it correct
Thanks
Prakash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2017 07:21 PM
Though I achieved what I wanted through a workaround, was wondering if there is a way I can block certain dates/days on the calendar for e.g. for a date/time field, when the user clicks on calendar icon next to it, it should not allow the user the select the past dates
another e.g. when the user is selecting an end date it should allow the user to select only dates that are after the start date.
This functionality with date/time fields is mostly seen on flight/hotel reservation and banking sites.
Anybody tried to achieve this before in ServiceNow?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2017 01:15 AM
Perhaps a Reference Qualifier that restricts lookup options. Not tried it, but that's the first place I'd look.