SharePoint Date Conversion to Javascript MM/DD/YYYY Format

To covert SharePoint Date to JavaScript Date Format MM/DD/YYYY use below code.


<script language="javascript" type="text/javascript">
/* ----------- SharePoint Date To JavaScript Date Format Code Start -------------*/
function convertSPDate(d) {
/*
* A function to convert a standard SharePoint
* date/time field (YYYY-MM-DD HH:MM:SS) to a
* javascript Date() object
*
* Author: Ben Tedder (www.bentedder.com)convertSPDate
*/
// split apart the date and time
var xDate = d.split(" ")[0];
var xTime = d.split(" ")[1];

// split apart the hour, minute, & second
var xTimeParts = xTime.split(":");
var xHour = xTimeParts[0];
var xMin = xTimeParts[1];
var xSec = xTimeParts[2];

// split apart the year, month, & day
var xDateParts = xDate.split("-");
var xYear = xDateParts[0];
var xMonth = xDateParts[1]-1;
var xDay = xDateParts[2];

var dDate = new Date(xYear, xMonth, xDay, xHour, xMin, xSec);
return dDate;
}
/* ----------- SharePoint Date To JavaScript Date Format Code End -------------*/

</script>

We also require moment.js to format it in MM/DD/YYYY Format


var DOB = new convertSPDate($(this).attr("ows_DOB"));
$("#txtDOB").val(moment(DOB).format('L'));

Moment JS Reference URL:
http://momentjs.com/
http://momentjs.com/downloads/moment.min.js

One thought on “SharePoint Date Conversion to Javascript MM/DD/YYYY Format

  1. This works great! Thank you!

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 )

Connecting to %s