- 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 09:00 AM
Just had a brief play around... if there's a date field on a form, using g_form.getValue() actually returns text held in that field - which will have already been offset by the user's locale and transformed by their display preferences, so it's already "tainted" by the time I come to process it in a client script.
This means that to process this as a Date object, we'll need to get the "untarnished" value from the database first, then process that accordingly. It's kinda reinventing the wheel since this is what is already taking place as part of the platform's normal operational functionality, however we need to "go to the source" here. Once that's obtained, it can be processed as a Date object (adding/removing days, etc) then displayed however you see fit.
So, there are two ways to address this:
- use some callback or AJAX function that obtains the unlocalised date in a format that can be easily parsed as a date object
- do the coding server-side using server APIs (taking advantage of GlideDate and GlideDateTime classes).
Having said all that... I've just had a read of your original problem definition and realised that a "After Query" BR ought to do what you want - which is pre-population of specific user-changable fields based upon data that already exists. Is this right?
Similarly, in terms of your business logic (startDate must be earlier than endDate, etc) - this could be achieved through a beforeUpdate/beforeInsert BR. It means a submit/update/save for the logic to kick in, but it'd definitely reject values that don't match your required criteria.
That help at all? I'm still toying with playing around with date objects, but conscious of the fact you still have a problem to solve and my experimentation may not advance your situation any.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2017 07:14 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 12:54 AM
Hi Paul,
I have tried to run ur code but i got expected result:
var tssd = new Date('2010-10-11T00:00:00+05:30');
var tssd_frmt = ('0'+(tssd.getMonth()+1)).slice(-2) + '-' + ('0'+ tssd.getDate()).slice(-2) + '-' + tssd.getFullYear();
alert(tssd_frmt );
result: 10-11-2010
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 07:26 AM
Thanks Prakash. nana
Did you try getting the date from a variable i.e. instead of passing '2010-10-11T00:00:00+05:30' as parameter to new Date(), did you try passing a variable or field as a parameter?
I will try appending "T00:00:00-04:00" and see what happens.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 07:31 AM
Hi Paul,
can u please post the date which you are getting from field.So that i can check.
Thanks