Retrieve Client's Date & Time

Is there a way to retrieve the client's date and time using ASP.NET 2.0?
Ray TurnerSenior ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pratima PharandeCommented:
CWS (haripriya)Commented:
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

gnoonCommented:
You can use a client script, such as javascript or vbscript, to get the client's datetime and send to server via a variable.
For example

<script language=javascript>
function getDateTime(frmObj)
{
    frmObj.cdatetime.value = new Date().toUTCString(); // Mon, 28 Jan 2008 06:10:54 UTC for exam
}
</script>
...
<form onsubmit="getDateTime(this)">
<input type=hidden name=cdatetime>
</form>

Then parse it at server like this

Dim clientDateTime As System.DateTime = System.DateTime.MinValue;
Dim culture As IFormatProvider = new System.Globalization.CultureInfo("en-US", true)
Try
     clientDateTime = System.DateTime.ParseExact(strD, "ddd, dd MMM yyyy HH:mm:ss 'UTC'", culture)
Catch
End Try

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gnoonCommented:
Dim strD As String = Request("cdatetime") '^^
Ray TurnerSenior ConsultantAuthor Commented:
Gnoon,

I tried your code and it returns a time that is 5 hours ahead of my time.  I was expecting to see the same date and time that is on my PC.

How do I get it to do that?
gnoonCommented:
I guess it's converted to UTC while parsing because we does not provide any time zome information in the format for the parsing.
Furthermore, as I see your time zone above it is GMT-05:00. Also, en-US (as we used it for parsing) is GMT-05:00. So, I'm not sure here which one is the cause of 5 hours ahead.

I think it's about DateTimeStyles of parsing. Unfortunately, I do not have the environment to test it.
I think using AssumeLocal option is forcing it to not convert to UTC again (because the javascript already did it).

Try
     clientDateTime = System.DateTime.ParseExact(strD, "ddd, dd MMM yyyy HH:mm:ss 'UTC'", culture, _
                  DateTimeStyles.AssumeLocal)
Catch
End Try
Ray TurnerSenior ConsultantAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.