SimpleDateFormat in javascript

Posted: 3 November 2009 in Javascript
/**
 * SimpledateFormat javascript class
 *
 * var reverseDateFormat = new SimpleDateFormat("YYYYMMdd");
 * var reverseDate = reverseDateFormat.format(new Date());
 * http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
 *
 * **/
function SimpleDateFormat(formatString){
 this.formatString = formatString;
 this.monthNames = ["January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
 this.dayNames =   ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

 this.format = function(aDate){
 var localFormatString = formatString;
 // The order is significant
 /* YYYY */  localFormatString = localFormatString.replace(/Y{3,}/g,     "\" + (aDate.getFullYear()) + \"");
 /* YY   */  localFormatString = localFormatString.replace(/Y{2}/g,      "\" + ((aDate.getFullYear()).toString().substring(2)) + \"");
 /* MMMM */  localFormatString = localFormatString.replace(/M{4,}/g,     "\" + (this.monthNames[aDate.getMonth()]) + \"");
 /* MMM  */  localFormatString = localFormatString.replace(/M{3}/g,      "\" + ((this.monthNames[aDate.getMonth()]).substring(0,3)) + \"");
 /* MM   */  localFormatString = localFormatString.replace(/M{2}/g,      "\" + (aDate.getMonth()+101).toString().substring(1) + \"");
 /* ww   */  /* don't have time to implement this today */
 /* WW   */  /* don't have time to implement this today */
 /* DD   */  /* don't have time to implement this today */
 /* dd   */  localFormatString = localFormatString.replace(/d{2}/g,      "\" + (aDate.getDate()+100).toString().substring(1) + \"");
 /* FF   */  localFormatString = localFormatString.replace(/F{2}/g,      "\" + (aDate.getDay()+100).toString().substring(1) + \"");
 /* EEEE */  localFormatString = localFormatString.replace(/E{4,}/g,     "\" + (this.dayNames[aDate.getDay()]) + \"");
 /* EEE  */  localFormatString = localFormatString.replace(/E{3}/g,      "\" + ((this.dayNames[aDate.getDay()]).substring(0,3)) + \"");
 /* EE   */  localFormatString = localFormatString.replace(/E{2}/g,      "\" + (aDate.getDay()+100).toString().substring(1) + \"");
 /* a    */  /* don't have time to implement this today */
 /* HH   */  localFormatString = localFormatString.replace(/H{2}/g,      "\" + (aDate.getHours()+100).toString().substring(1) + \"");
 /* kk   */  localFormatString = localFormatString.replace(/k{2}/g,      "\" + (aDate.getHours()+101).toString().substring(1) + \"");
 /* KK   */  /* don't have time to implement this today */
 /* hh   */  /* don't have time to implement this today */
 /* mm   */  localFormatString = localFormatString.replace(/m{2}/g,      "\" + (aDate.getMinutes()+100).toString().substring(1) + \"");
 /* ss   */  localFormatString = localFormatString.replace(/s{2}/g,      "\" + (aDate.getSeconds()+100).toString().substring(1) + \"");
 /* SS   */  localFormatString = localFormatString.replace(/S{2}/g,      "\" + (aDate.getMilliSeconds()+1000).toString().substring(1) + \"");
 /* z   */   /* don't have time to implement this today */

 localFormatString = "\"" + localFormatString + "\"";
 //prompt("localFormatString", localFormatString);
 var formatedDate = eval(localFormatString);
 return(formatedDate);
 }
}
Advertisements
Comments
  1. […] DatasourcesJuly 2010 2 comments 2 Interview QuestionsMarch 2010 1 comment 3 SimpleDateFormat in javascriptNovember 2009 4 What are Teamsite, FormsPublisher, SitePublisher, Livesite, OpenDeploy and […]

  2. Lucas says:

    Was this class ever completed?

  3. No, I never had to revisit this class. what i made it do was sufficient for the formats I wanted to support.

  4. […] code, and I just wanted to shove in the format string as an option.  So with much thanks to this post by Laurent Picquet I extended it to meet my needs.  The code I wrote is far from […]

  5. candanny says:

    Hi. Thank you very much, helped me solve a problem I was stuck on. I took your code and converted it into a much dumber but useful function for use with jQueryUI datepicker. See my post on it here: http://wp.me/p1IRyT-B

  6. Ivy says:

    Skype has opened its website-structured client beta on the entire world, soon after launching
    it largely from the U.S. and U.K. before this four weeks. Skype for Web also now facilitates
    Chromebook and Linux for instant online messaging conversation (no video and voice nevertheless, those demand a connect-in installation).

    The increase from the beta provides help for an extended selection of
    different languages to help you reinforce that international functionality

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s