Wednesday, July 31, 2013

Javascript function for converting a datetime object to a specific format

Chances are very good that at some point during your journey with SharePoint 2013 you're going to need a javascript function that you can use for converting a datetime object into a nice, user-friendly output string for display purposes.  In the code sample I'm about to provide, the javascript function will return an output string that matches the following format:

Wednesday, 7/13/2013

Here's the basic function that will give you the output presented above:

function getDateString(dateTime) {
 var weekday=new Array();
 weekday[0]="Sunday";
 weekday[1]="Monday";
 weekday[2]="Tuesday";
 weekday[3]="Wednesday";
 weekday[4]="Thursday";
 weekday[5]="Friday";
 weekday[6]="Saturday";

 var dayOfWeek = weekday[dateTime.getDay()];
 var month = dateTime.getMonth() + 1;
 var day = dateTime.getDate();
 var year = dateTime.getFullYear();

 return dayOfWeek + ', ' + month + '/' + day + '/' + year;
}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.