1

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!

3 Answers 3

2

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")
Sign up to request clarification or add additional context in comments.

1 Comment

The ISO format is supported by default, so this shouldn't have made any difference. See "Supported ISO 8601 strings" under this part of the docs. Also, you are missing the T, but that wouldn't make a difference either.
1

You can build it like this

var date1 = "2015-12-05T00:00:00";
date1.replace('T',' ');
var date2 = new Date(date1);
var yyyy = date2.getFullYear().toString();
var mm = (date2.getMonth()+1).toString();
var dd  = date2.getDate().toString();
var finaldate = mm+'/'+dd+'/'+yyyy;

1 Comment

A couple of issues with this approach: 1) you'd need to zero-pad the mm and dd values. 2) the Date object might parse it as UTC instead of local time, which could throw it a day earlier depending on time zone. moment is much safer.
-1
$("#BoardStart").datepicker("setDate", new Date(data.BoardStart));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.