Knowledge Base Article

> Knowledge Base > Rapise

Created By  alexey.grinevich Monday, March 27, 2017

This is a short sample demonstrating how one can format a date

JavaScript has own dedicated Date object.  However it does not provide own formatting options so there are plenty of examples available shows different approaches on printing a formatted date.  Here is show one of them covering formatting as ISO8601 string. You may modify this function to fit your own needs:

// Put this function into Function (.user.js) file 
function toISOString(date) {
	date = date || new Date();
	function pad(number) {
		var r = String(number);
		if ( r.length === 1 ) {
		r = '0' + r;
		}
		return r;
	}
	return date.getFullYear()
		+ '-' + pad( date.getMonth() + 1 )
		+ '-' + pad( date.getDate() )
		+ 'T' + pad( date.getHours() )
		+ ':' + pad( date.getMinutes() )
		+ ':' + pad( date.getSeconds() )
		+ '.' + String( (date.getMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
		+ 'Z';
}

 

 

1. Print Today's date in ISO8601 format:

	// Now you may use it like that:
	// Current date:
	var today = new Date();
	var isoDate = toISOString(today)
	Tester.Message("Iso date: "+isoDate);

2. Print Today's date in short format yyyy-mm-dd:

	// Short version of the date - cut 1st 10 symbols so we have yyyy-mm-dd:
	var today = new Date();
	var yyyymmdd= toISOString(today).substring(0, 10);
	Tester.Message("yyyymmdd: "+yyyymmdd);

3.  Print specific date:

	// Specific date (2015-04-01). 
	// Note that month is 0-based, so 0 is Jan, 3 is April.
	var someDate = new Date(2015, 3, 1);
	Tester.Message("Specific date: "+toISOString(someDate));

4.  Print specific date and time:

	// Specific date and time (2012-10-05T20:45). 
	// Note that month is 0-based, so 0 is Jan, 3 is April.
	var someDateTime = new Date(2012, 9, 5, 20, 45);
	Tester.Message("Specific date and time: "+toISOString(someDateTime));

 

 

 

 

 

Article Info
  • Last Updated: 3/27/2017
  • Article ID: KB247
  • Views: 494