Formatting date using Javascript

Post Reply
reprokaiser
Newbie
Posts: 16
Joined: Mon Apr 04, 2011 3:56 pm

Formatting date using Javascript

Post 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
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Formatting date using Javascript

Post 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
reprokaiser
Newbie
Posts: 16
Joined: Mon Apr 04, 2011 3:56 pm

Formatting date using Javascript

Post 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
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Formatting date using Javascript

Post 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
reprokaiser
Newbie
Posts: 16
Joined: Mon Apr 04, 2011 3:56 pm

Formatting date using Javascript

Post 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
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Formatting date using Javascript

Post by dkelly »

You can use "[Job.FileCreationDate:Format=yyyy MMMM d]" as a variable.



Dwight Kelly

Apago, Inc.

dkelly@apago.com
Post Reply