1

There is an MVC 4 Web Application deployed on a server with specific date format (ex. US date format 02/02/2015).

How can i display a date on the client machine by respecting his local date culture (ex. Uk format 02-02-2015) so that when I retrive from the server the value 02/02/2015 on the client side it will be 02-02-2015?

I tried to use:

date.toLocaleDateString()

but whatever format i choose for my client machine the output remains the same.

Or if it is possible, how can i get the clients system date to pass it to my controller without asking to choose a format?

wookiee
  • 110
  • 1
  • 3
  • 11

1 Answers1

1

In a situation like that, it would probably be best to send over the date in ticks.

Per this Stack Overflow answer, you can take your DateTime object, convert it to UTC, and then convert it back to a date in the client.

modelDate = myDate.ToUniversalTime()
                  .Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
                  .TotalMilliseconds();

Then just pass it into a JavaScript variable on your page like so:

var date = new Date(@model.Date);
// Then assign it to whatever element with jQuery or JavaScript.
$('#dateBox').val(date);
Community
  • 1
  • 1
krillgar
  • 12,596
  • 6
  • 50
  • 86