Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I created one website and hosted it, so i need how to get client machine time using C# when client sign in there login status. i tried Datetime.now method but this method get server time only. so please help how can i get client machine time using c#,

(or)
Timezoneinfo Does not support in my code, how can i add Timezoneinfo method in c# coding to convert time server to local.
Posted

You can't get the time form the client PC using C# because the code-behind is executed on the SERVER, not in the client. You canuse JavaScript to get the current time from the browser.
Have a look on the following links
LINK1
LINK2
 
Share this answer
 
Comments
prabaran 21-Aug-12 1:31am    
Thanks prabha,
I used the following Script it will display the current machine time. But i couldn't get the value in label and couldn't stored it database. Help me.

Script is,

It is now
<script type="text/javascript">
<!--
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
minutes = "0" + minutes
}
document.write(hours + ":" + minutes + " ")
if(hours > 11){
document.write("PM")
} else {
document.write("AM")
}
//-->
</script>

Hi,

There is no way to do it in C#, you have to get the time offset in javascript:
JavaScript
<script type="text/javascript">
    

     function GetLocalTimeOffset(id)
     {
       var now = new Date();
       var offset = now.getTimezoneOffset();
        

        var hidTimeZone = document.getElementById(id);
        if(hidTimeZone != null)
        {
            hidTimeZone.value = offset;
        }
     }
     

    </script>

Store it in session by using the hidden field, and after you can get the date with some methods like this (look my helper class):
C#
public static class DateTimeHelper
    {
        private const string dateTimeStringFormat = "dateTimeStringFormat";
        private const string timeStringFromat = "timeStringFormat";
        private const string dateStringFormat = "dateStringFormat";

        public static string GetDate(DateTime date)
        {
            string format = ConfigurationManager.AppSettings[dateStringFormat];
            return date.ToString(format, new CultureInfo("en-US"));
        }

        public static string GetDate(DateTime date, double timeZoneOffset)
        {
            string format = ConfigurationManager.AppSettings[dateStringFormat];
            return ConvertUtcToLocal(date, timeZoneOffset).ToString(format, new CultureInfo("en-US"));
        }

        public static string GetTime(DateTime time, double timeZoneOffset)
        {
            string format = ConfigurationManager.AppSettings[timeStringFromat];
            return ConvertUtcToLocal(time, timeZoneOffset).ToString(format, new CultureInfo("en-US"));
        }

        public static string GetDateTime(DateTime dateTime, double timeZoneOffset)
        {
            string format = ConfigurationManager.AppSettings[dateTimeStringFormat];
            return ConvertUtcToLocal(dateTime, timeZoneOffset).ToString(format, new CultureInfo("en-US"));
        }

        private static DateTime ConvertUtcToLocal(DateTime dateTime, double timeZoneOffset)
        {
            //Move to universal time (GMT) with Zero offset 
            DateTime utcDateTime = dateTime.ToUniversalTime();
            //Add client side offset
            return utcDateTime.AddMinutes((-1) * timeZoneOffset);
        }
    }

Regards
sham
 
Share this answer
 
v2
Comments
prabaran 20-Aug-12 3:55am    
hi sham,

i couldn't understand please explain briefly or give me your demo page.
Shambhoo kumar 20-Aug-12 4:17am    
I have demo page but not in web .it is in my local machin . then how can i show demo..
hii,
to get machine time use javascript

http://www.tizag.com/javascriptT/javascriptdate.php[^]
 
Share this answer
 
Comments
prabaran 21-Aug-12 1:27am    
hai i used this script. but i couldn't get the time in Label to stored in database. So how can i get the time in Label.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900