Thursday, July 31, 2014

Javascript Date Functions

July 31st 2014.

Lot of people are afraid to deal with date objects in JavaScript. While it is not the easiest to deal with it is not certainly the most difficult to deal with.  With little time and experiments and lots of patience you can understand how it works.  Once learnt building something additional to it is lot easier in latter times.  Most of the functions I learnt in JavaScript is by studying W3Schools articles.
You can view all of their offering at www.w3schools.com

Today I am going to show you an example of date object manipulation.  This is in response to a discussion in the Alpha Message Board. In the following weeks I will post how the date object works as told by w3schools.

Think of a scenario where a vendor sends you a bill. The bill may be due in various dates according to the terms established by the vendor, for example it may be due on delivery, 10 days from delivery, certain day of the month or end of the month etc.,

So I have created a dialog in Alpha Five that will deal with three fields. Date of invoice, terms and due date.  Since this is only to show the date object functionality, I have designed that way.  In real world the terms will be at the vendor file and it will populate when you select the vendor and the due date will be calculated and assigned when you select the invoice date. You may be able to change the terms on the fly with the option of changing it permanently in the vendor table or just for this particular invoice.  I have not shown that since that is not planned.  The example deals with how the date object is manipulated, that is it.  You can use your imagination to build more complex component, if you wish.

In this example I have three fields. Date of Invoice, Terms and Due Date. The field terms is a drop-down with three options. The options are
(1) due 10th day of month returned value 1
(2) end of the month returned value 2
(3) 10 days from the bill returned value 3
The date of invoice is entered and in the onChange event of the field Terms I have written the inline JavaScript code that fires and calculates the due date according to the terms picked. It is important to notice that the dates are good even for leap years and leap centuries.

So here is the video:
http://screencast.com/t/mn0CtPesHq

the code that runs in the onChange event of the field Terms is here:
As I said earlier this is only for demonstration, you can assign the various values as you like and arrive at your desired result.

var invDate = {dialog.Object}.getValue('DATE_OF_INVOICE');
invDate = new Date(invDate);
var terms = {dialog.Object}.getValue('TERMS');
if (terms == 1) {
//alert('10th of the month');
var due_day = 10;
var d1 = invDate.getDate();
var m1 = invDate.getMonth()+1;
//
if (d1 <= due_day) {
var new_invDate = invDate.setDate(due_day);
}
else
{
new_invDate = invDate.setDate(due_day);
new_invDate = invDate.setMonth(m1);
}
invDate.setTime(new_invDate);
var mm = invDate.getMonth()+1;
if (mm < 10) {
mm = "0"+mm;
}
var dd = invDate.getDate();
var yyyy =invDate.getFullYear();
}
else if (terms == 2) {
//alert('end of the month');
var lastDayOfMonth = new Date(invDate.getFullYear(),invDate.getMonth()+1,0);
var mm = lastDayOfMonth.getMonth()+1;
var dd = lastDayOfMonth.getDate();
var yyyy = lastDayOfMonth.getFullYear();
}
else if (terms == 3) {
//alert('10 days from invoice');
invDate.setTime(invDate.getTime()+(10*24*60*60*1000));
var mm = invDate.getMonth()+1;
var dd = invDate.getDate();
var yyyy = invDate.getFullYear();
}
var due_date = mm + "/" + dd + "/" + yyyy;
{dialog.Object}.setValue('DUE_DATE',due_date );

Please let me know if there are any errors and any improvements I can make.