Dreamcasting Dreamcasting - 4 months ago 25
jQuery Question

Convert JSON.NET date to jQuery UI Datepicker format

I am serializing and passing a date out of my controller using

JsonConvert.SerializeObject
and sending it to a jQuery UI Datepicker field. Right now the output is like
"2015-12-05T00:00:00"
and I want it to be in a format of
MM/DD/YYYY
. My output is available via the AJAX returned data object
data.BoardStart
and is being attached to
#BoardStart
but always shows as 12/31/1969.

I've tried moment (and failed) using this code:

var BoardStart = data.BoardStart;
$("#BoardStart").val(moment(BoardStart).format('MM/DD/YYYY'));


Any suggestions? Thanks!

Answer

Using the moment.js library for formatting date, substitute

moment(BoardStart).format('MM/DD/YYYY')

with

moment(BoardStart, "YYYY-MM-DDhh:mm:ss").format("MM/DD/YYYY")