如何使用C#获得机器时间 [英] How to get Machine time Using C#
问题描述
大家好,
我创建了一个网站并进行了托管,因此当客户端登录那里的登录状态时,我需要如何使用C#获取客户端计算机时间.我尝试使用Datetime.now方法,但是此方法仅获取服务器时间.所以请帮助我如何使用C#获得客户端计算机的时间,
(或)
Timezoneinfo我的代码中不支持,如何在c#编码中添加Timezoneinfo方法以将时间服务器转换为本地.
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.
推荐答案
您无法获取时间格式客户端PC使用C#,因为后台代码是在SERVER上执行的,而不是在客户端上执行的.您可以使用JavaScript从浏览器获取当前时间.
看看以下链接
LINK1
LINK2
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
在C#中无法做到这一点,您必须在javascript中获取时间偏移:
Hi,
There is no way to do it in C#, you have to get the time offset in 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):
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
hii,
要获取机器时间,请使用javascript
http://www.tizag.com/javascriptT/javascriptdate.php [
hii,
to get machine time use javascript
http://www.tizag.com/javascriptT/javascriptdate.php[^]
这篇关于如何使用C#获得机器时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!