2

I'm trying to convert the javascript date to ASP.NET's DateTime.Now

var todaysDate = new Date();
document.getElementById('hdnDate').value = todaysDate.toString();


private void ConvertToDotNetDateTime()
{
    DateTime myDate = (DateTime)hdnDate.Value; ??? ? //bit lost here
}
Azhar
  • 20,500
  • 38
  • 146
  • 211
Dkong
  • 2,748
  • 10
  • 54
  • 73
  • 3
    Duplicate of http://stackoverflow.com/questions/1877788/javascript-date-to-c-via-ajax – EMP Sep 04 '10 at 06:13

2 Answers2

2

JavaScript:

document.getElementById('hdnDate').value = (new Date()).format('dd/MM/yyyy HH:mm:ss');

in C#:

CultureInfo provider = CultureInfo.InvariantCulture;

string date = hfClientDate.Value;

string format = "dd/MM/yyyy HH:mm:ss";

DateTime dt = DateTime.ParseExact(date, format, provider);

Hope this helps someone.

Cosmin.D
  • 21
  • 2
0

You can use DateTime.ParseExact to convert the date to DateTime.Now

You can check the Example

Azhar
  • 20,500
  • 38
  • 146
  • 211
  • -1. A lot of your answers seem to be comprised of "check the link" and that is not the point of SO - especially when the same link was already posted as a comment 40 minutes earlier. – EMP Sep 04 '10 at 07:40