Difference between revisions of "Manipulating Dates"
From SmartWiki
(11 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | The following [[JavaScript]] functions are built | + | {{JavaScript Syntax}} |
+ | The following [[JavaScript]] functions are built in to SmartSimple to assist when working with and performing calculations using dates: | ||
==ConvertDateStr== | ==ConvertDateStr== | ||
Line 25: | Line 26: | ||
'''Example:''' | '''Example:''' | ||
var newDate=ConvertDate(sdate,'@dateformat@'); | var newDate=ConvertDate(sdate,'@dateformat@'); | ||
+ | |||
+ | |||
+ | ==ssConvertDate== | ||
+ | Takes a date in any date format and converts it to any other date format. | ||
+ | |||
+ | '''Syntax:'''<br> | ||
+ | :<font size="3">'''ssConvertDate(''date'','fromdateformat','todateformat')'''</font> | ||
+ | |||
+ | '''Where:''' | ||
+ | * ''date'' is the date you wish to change to the users preferred display format. | ||
+ | * ''fromdateformat'' is the format of the date you are providing | ||
+ | * ''todateformat'' is the format you want the resulting date to use. | ||
+ | |||
+ | '''Example:''' | ||
+ | var newDate=ssConvertDate(sdate,'yyyy-mm-dd','dd/mm/yyyy'); | ||
+ | or | ||
+ | var newDate=ssConvertDate(sdate,'mm/dd/yyyy','dd-mm-yyyy'); | ||
Line 38: | Line 56: | ||
::* 'd' (days) | ::* 'd' (days) | ||
::* 'm' (months) | ::* 'm' (months) | ||
− | ::* ' | + | ::* 'yyyy' (years) |
* ''interval'' is the number of days/months/years to be added. | * ''interval'' is the number of days/months/years to be added. | ||
'''Example:''' | '''Example:''' | ||
− | var newDate= | + | var newDate=dateadd(sdate,'m',duration); |
− | |||
− | ==Example== | + | ===Example=== |
The following example adds a specified number of months (saved in cf_123456) to the startdate, and saves it as the enddate. | The following example adds a specified number of months (saved in cf_123456) to the startdate, and saves it as the enddate. | ||
Line 56: | Line 73: | ||
} | } | ||
var edate=dateadd(sdate,'m',duration); | var edate=dateadd(sdate,'m',duration); | ||
− | frm.enddate.value= | + | frm.enddate.value=ssConvertDate(edate,"@dateformat@"); |
</pre> | </pre> | ||
+ | |||
==Notes== | ==Notes== | ||
* When using [[JavaScript]] to populate a date it must be added to the field in the preferred date format of the current user, as the system will automatically convert it to yyyy-mm-dd when the page is saved. | * When using [[JavaScript]] to populate a date it must be added to the field in the preferred date format of the current user, as the system will automatically convert it to yyyy-mm-dd when the page is saved. | ||
+ | |||
+ | * To change the display format of dates displayed on the page see [[sscalculation]] | ||
Line 68: | Line 88: | ||
* [[isHoliday]] | * [[isHoliday]] | ||
* [[Custom Defined Holidays]] | * [[Custom Defined Holidays]] | ||
+ | * [[Sscalculation]] | ||
+ | * [[Template_/_Type_Formula#Counting_and_Adding_Business_Days|Using Template Formulas to Count and Add Business Days]] | ||
+ | |||
[[Category:JavaScript]] | [[Category:JavaScript]] | ||
+ | [[Category:validate.js]][[Category:Date Formats]] |
Latest revision as of 10:13, 4 April 2014
This feature uses JavaScript syntax |
The following JavaScript functions are built in to SmartSimple to assist when working with and performing 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@');
ssConvertDate
Takes a date in any date format and converts it to any other date format.
Syntax:
- ssConvertDate(date,'fromdateformat','todateformat')
Where:
- date is the date you wish to change to the users preferred display format.
- fromdateformat is the format of the date you are providing
- todateformat is the format you want the resulting date to use.
Example:
var newDate=ssConvertDate(sdate,'yyyy-mm-dd','dd/mm/yyyy');
or
var newDate=ssConvertDate(sdate,'mm/dd/yyyy','dd-mm-yyyy');
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)
- 'yyyy' (years)
- interval is the number of days/months/years to be added.
Example:
var newDate=dateadd(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=ssConvertDate(edate,"@dateformat@");
Notes
- When using JavaScript to populate a date it must be added to the field in the preferred date format of the current user, as the system will automatically convert it to yyyy-mm-dd when the page is saved.
- To change the display format of dates displayed on the page see sscalculation