Difference between revisions of "Manipulating Dates"

From SmartWiki
Jump to: navigation, search
(Created page with 'The following JavaScript functions are built-in to SmartSimple to assist with manipulation and calculations using dates: ==ConvertDateStr== '''Syntax:'''<br> :<font size="3"…')
 
Line 2: Line 2:
  
 
==ConvertDateStr==
 
==ConvertDateStr==
 +
Takes a date that is in the users preferred display format and returns the date in the standard database format of yyyy-mm-dd.
 +
 
'''Syntax:'''<br>
 
'''Syntax:'''<br>
:<font size="3">'''ConvertDateStr(date,'@dateformat@')'''</font>
+
:<font size="3">'''ConvertDateStr(''date'','@dateformat@')'''</font>
 +
 
 +
'''Where:'''
 +
* ''date'' is the date you wish to change to the ''yyyy-mm-dd' format.
  
 
'''Example:'''
 
'''Example:'''
  ConvertDateStr(frm.startdate.value,'@dateformat@');
+
  var newDate=ConvertDateStr(frm.startdate.value,'@dateformat@');
 +
 
 +
 
 +
==ConvertDate==
 +
Takes a date that is in the standard database format of yyyy-mm-dd and converts it to the preferred display format of the current user.
 +
 
 +
'''Syntax:'''<br>
 +
:<font size="3">'''ConvertDate(''date'','@dateformat@')'''</font>
 +
 
 +
'''Where:'''
 +
* ''date'' is the date you wish to change to the users preferred display format.
  
 +
'''Example:'''
 +
var newDate=ConvertDate(sdate,'@dateformat@');
  
  
 +
==Notes==
 +
* When populating a date field that is displayed on the page it must be added in the preferred date format of the current user, as the system will automatically change it to yyyy-mm-dd when the page is saved.
 +
 +
 +
==dateadd==
 +
Add the specified interval to a date
 +
 +
'''Syntax:'''<br>
 +
:<font size="3">'''dateadd(''date'',''unit'',''interval'')'''</font>
 +
 +
'''Where:'''
 +
* ''date'' is the date you wish to add to (or delete from) in format yyyy-mm-dd
 +
* ''unit'' specifies what is being added:
 +
::* 'd' (days)
 +
::* 'm' (months)
 +
::* 'y' (years)
 +
* ''interval'' is the number of days/months/years to be added.
 +
 +
'''Example:'''
 +
var newDate=ConvertDate(sdate,'m',duration);
  
  
 +
==Example==
 +
The following example adds a specified number of months (saved in cf_123456) to the startdate, and saves it as the enddate.
  
 +
<pre>
 +
var sdate=ConvertDateStr(frm.startdate.value,'@dateformat@');
 +
var duration=parseInt(parseFloat(frm.cf_123456.value));
 +
if(isNaN(duration)){
 +
  alert("Invalid Duration provided");
 +
  return;
 +
  }
 +
var edate=dateadd(sdate,'m',duration);
 +
frm.enddate.value=ConvertDate(edate,"@dateformat@");
 +
</pre>
  
  
 
[[Category:JavaScript]]
 
[[Category:JavaScript]]

Revision as of 12:34, 16 August 2010

The following JavaScript functions are built-in to SmartSimple to assist with manipulation and calculations using dates:

ConvertDateStr

Takes a date that is in the users preferred display format and returns the date in the standard database format of yyyy-mm-dd.

Syntax:

ConvertDateStr(date,'@dateformat@')

Where:

  • date is the date you wish to change to the yyyy-mm-dd' format.

Example:

var newDate=ConvertDateStr(frm.startdate.value,'@dateformat@');


ConvertDate

Takes a date that is in the standard database format of yyyy-mm-dd and converts it to the preferred display format of the current user.

Syntax:

ConvertDate(date,'@dateformat@')

Where:

  • date is the date you wish to change to the users preferred display format.

Example:

var newDate=ConvertDate(sdate,'@dateformat@');


Notes

  • When populating a date field that is displayed on the page it must be added in the preferred date format of the current user, as the system will automatically change it to yyyy-mm-dd when the page is saved.


dateadd

Add the specified interval to a date

Syntax:

dateadd(date,unit,interval)

Where:

  • date is the date you wish to add to (or delete from) in format yyyy-mm-dd
  • unit specifies what is being added:
  • 'd' (days)
  • 'm' (months)
  • 'y' (years)
  • interval is the number of days/months/years to be added.

Example:

var newDate=ConvertDate(sdate,'m',duration);


Example

The following example adds a specified number of months (saved in cf_123456) to the startdate, and saves it as the enddate.

var sdate=ConvertDateStr(frm.startdate.value,'@dateformat@');
var duration=parseInt(parseFloat(frm.cf_123456.value));
if(isNaN(duration)){
   alert("Invalid Duration provided");
   return;
   }
var edate=dateadd(sdate,'m',duration);
frm.enddate.value=ConvertDate(edate,"@dateformat@");