3

I am making a web application, and I need a DateTime format that is both compatible with JS Date and .NET DateTime, which are outlined differently on the following two pages:

JS Format: http://blog.stevenlevithan.com/archives/date-time-format

.NET Format: http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo(v=vs.71).aspx

As you can see, they are similar, but not exactly the same. The one I would like to use in JS is "mmmm dS, yyyy, h:MM:ss TT", but there is no equivalent of this format in .NET much to my dismay. Is there any other format I could use that would work in both JS and .NET without any need of reformatting when passed between the two languages?

Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174

2 Answers2

1

Surely its a relatively straight forward jobs to standardize the formatting flags between .net and JS?

Update based on comment

The OP wanted to format dates in JS like so:

dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"); // Saturday, June 9th, 2007, 5:46:21 PM

To format a date in a similar format in .NET you could do something like: (more info here https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx)

myBirthday.ToString("dddd, MMMM dd, yyyy, h:MM:ss tt");
// Saturday, June 9, 2007, 5:46:21 PM

You'll see the day doesn't include the ordinal suffix - for that refer to Is there an easy way to create ordinals in C#?

Community
  • 1
  • 1
Keir
  • 483
  • 3
  • 10
-1

When using date objects between two systems, the ideal approach is to use the ISO 8601 format. Unfortunately not all JS implementations support ISO 8601 (Webkit-based ones for example).

This blog has a good idea. Add an object to the date objects prototype, to handle ISO 8601 dates correctly.

CD Jorgensen
  • 1,361
  • 9
  • 8