Page 1 of 1
					
				Formatting date using Javascript
				Posted: Fri Apr 22, 2011 7:24 pm
				by reprokaiser
				Dear Sirs,
	
	please help me format date information using Javascript in PowerSwitch. I'm trying to present directory data to users in a form which is comfortable them to use. Currently all I can do is to show them the ISO formatted data (1998-12-23T12-43-45), which is fine, but is a bit too much for now. I know that there is a function to format the date using a format string, but I can't find the usage in the manual, I don't know how to apply it. So:
	
	var theFile = new File( "/HD/Folder/File" );
	var theDate = theFile.created;
	???
	
	What can I don to format that date to something like 1998 December 23?
	
	Thanks in advance!
	
	Peter
			 
			
					
				Formatting date using Javascript
				Posted: Fri Apr 22, 2011 8:28 pm
				by dkelly
				The Date class has a function called toString() that converts to a string in ISO 8601 format, eg. YYYY-MM-DDTHH:MM:SS.
	
	
	var theFile = new File( "/HD/Folder/File" );
	var theDate = theFile.created;
	var dateString = theDate.toString();
	
	
	The code above would produces a string similar to "2011-04-22T14:21:00"
	
	If you want to generate a string like your example, then do something like:
	
	var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", Dec ]; 
	
	var theFile = new File( "/HD/Folder/File" );
	var theDate = theFile.created;
	var dateString = theDate.getYear() + " " + months[theDate.getMonth()-1] + " " + theDate.getDay();
	
	
	Dwight Kelly
	Apago, Inc.
	
dkelly@apago.com 
			 
			
					
				Formatting date using Javascript
				Posted: Fri Apr 22, 2011 8:40 pm
				by reprokaiser
				Hi dkelly,
	
	thanks for the quick reply. I know that I can fabricate a string using methods like you showed, just I thought that there is a built-in feature to do that, much more easily. Please take a look into the Scripting Reference (search: formatting, then click on the link: Formatting, then scroll a bit down until Date). Here it is: 
	
	Format="format-string"
	
	String representation in the format specified by the format string, which must conform to the syntax described below
	
	After that there is a key about how to build the "format-string". Am I wrong thinking that there is a simple function (albeit rarely used) to perform what I need?
	
	Kind regards,
	
	Peter
			 
			
					
				Formatting date using Javascript
				Posted: Fri Apr 22, 2011 11:52 pm
				by dkelly
				OK, this works using variable formatting syntax.
	
	 var dateString = job.getVariableAsString("[Job.FileCreationDate:Format=yyyy MMMM d]", s);
	
	
	Dwight Kelly
	Apago, Inc.
	
dkelly@apago.com
 
			 
			
					
				Formatting date using Javascript
				Posted: Tue Apr 26, 2011 11:34 am
				by reprokaiser
				Hi dkelly,
	
	many thanks for the answer. I wonder is it possible to use this very handy compact form in case of any file, not just the 'job' itself?
	
	Kind regards,
	
	Peter
			 
			
					
				Formatting date using Javascript
				Posted: Tue Apr 26, 2011 4:29 pm
				by dkelly
				You can use "[Job.FileCreationDate:Format=yyyy MMMM d]" as a variable.
	
	Dwight Kelly
	Apago, Inc.
	
dkelly@apago.com