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
}
1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

You can check the Example

1 Comment

-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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.