How can I format a javascript date to be serialized by jQuery
Asked Answered
D

3

6

I am trying to set a javascript date so that it can be submitted via JSON to a .NET type, but when attempting to do this, jQuery sets the date to a full string, what format does it have to be in to be converted to a .NET type?

var regDate = student.RegistrationDate.getMonth() + "/" + student.RegistrationDate.getDate() + "/" + student.RegistrationDate.getFullYear();
j("#student_registrationdate").val(regDate); // value to serialize

I am using MonoRail on the server to perform the binding to a .NET type, that aside I need to know what to set the form hidden field value to, to get properly sent to .NET code.

Duley answered 27/8, 2008 at 14:0 Comment(0)
M
2

This MSDN article has some example Date strings that are parse-able is that what you're looking for?

string dateString = "5/1/2008 8:30:52 AM";
DateTime date1 = DateTime.Parse(dateString, CultureInfo.InvariantCulture); 
Monostrophe answered 27/8, 2008 at 14:12 Comment(0)
U
2

As travis suggests, you could simply change the parameter or class property (depending on what you are passing back) to a string, the parse it as his example.

You may also want to take a look at this article. It suggests that direct conversion for DateTime JSON serialization uses something more like the ticks property.

Unitary answered 27/8, 2008 at 14:16 Comment(0)
A
2

I suggest you use the YYYY-MM-DD notation which offer the best combo of unambiguity and readability.

so:

var regDate = student.RegistrationDate.getFullYear() + "-" + student.RegistrationDate.getMonth() + "-" + student.RegistrationDate.getDate(); j("#student_registrationdate").val(regDate);

Avow answered 17/6, 2009 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.