Foster Hardie
Tera Expert

A quick pair of tips for dealing with date formats on the client side:

This is an undocumented function and global that will help you turn date strings pulled from form controls into JavaScript dates for date manipulation:

UNDOCUMENTED FUNCTION: getDateFromFormat(dateString, format)

UNDOCUMENTED GLOBAL: g_user_date_format

EXAMPLE: var jsDate = getDateFromFormat(g_form.getValue('a_date_field'), g_user_date_format)

And this is a quick function I put together to do the reverse (format a JavaScript date to a string pattern). Note that it does not format the time. That could be added easily. Also, moment.js is an entire library of date functions if you prefer. But this is lighter weight if you only need basic date formatting:

function formatDateString(dt, format) {
	var d = dt.getDate();
	var M = dt.getMonth() + 1;
	var yyyy = dt.getFullYear();
	var dd = ('0' + d).slice(-2);
	var MM = ('0' + M).slice(-2);
	var yy = ('0' + yyyy).slice(-2);
	
	var returnValue = format.replace('dd', dd);
	returnValue = returnValue.replace('d', d);
	returnValue = returnValue.replace('MM', MM);
	returnValue = returnValue.replace('M', M);
	returnValue = returnValue.replace('yyyy', yyyy);
	returnValue = returnValue.replace('yy', yy);
			
	return returnValue;
}

EXAMPLE: var dtString = formatDateString(new Date(), g_user_date_format)

Version history
Last update:
‎03-20-2019 10:56 AM
Updated by: