views:

93

answers:

1

Hi,

I need to pass a date, that will be generated via javascript to a asp.net http handler that is services in ajax requests.

On the .net side, I will take the date passed in a cast it to DateTime.

The date has to have: yyyy, mm, dd, and minutes and seconds.

How can I format a date in javascript to this format?

Using jquery

A: 

If you're using ASP.NET AJAX already in your project, Dave Ward explains how to use ASP.NET AJAX extensions for this purpose in his blog post Work smarter: MS AJAX's JavScript type extensions:

Simplify formatting client side dates with Date.format If you’ve spent much time working with dates in JavaScript, you know what a hassle it can sometimes be. To greatly ease this, ASP.NET AJAX extends the JavaScript Date object with a formatting function that closely mimics the ToString formatting paradigm that we’ve been using for years. Here are a few examples:

var today = new Date('12/3/2007');

var shortDate = today.format('d');
// d formats the date as MM/dd/yyyy
// shortDate == '12/03/2007'

var longDate = today.format('D');
// D formats the date as dddd, dd MMMM yyyy
// longDate == 'Monday, 03 December 2007'

var customDate = today.format('MMMM, yyyy');
// Custom format string to format the date as MMMM, yyyy
// customDate == 'December, 2007'

Date.format accepts most of the standard DateTime format strings, such as d and D. If none of those suit your needs, Date.format also provides almost unlimited flexibility via custom formatting strings. For a full list of formatting string parameters, see the Standard DateTime Format Strings and Custom DateTime Format Strings reference pages on MSDN.

Ian Robinson
I need minutes and seconds...
mrblah
the links at the bottom have all kinds of variations. For example: var myCustomDateTime = today.format('G'); // "09/27/2006 14:15:39"
Ian Robinson
How did that end up working out for you?
Ian Robinson