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?
 
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
0
 
Pratima PharandeCommented:
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
CWS (haripriya)Commented:
0
 
gnoonCommented:
Dim strD As String = Request("cdatetime") '^^
0
 
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?
0
 
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
0
 
Ray TurnerSenior ConsultantAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.