C# ingTime==supportsDst){ 如果(!supportsDst)zoneInfo数组.Add(zoneInfo); 否则{ //有DST。查找DST的第一天并测试是否与发送的值匹配。天=年内的天偏移量 int foundDay=0; DateTime janDate=新的日期时间(2016,1,1,12,0,0);//正午 int startDay=zoneInfo.IsDaylightSavingTime(janDate)?180:0;//如果是南半球,则从一年中的180天开始 对于(int day=startDay;day
对于Dot Net 3.5版及更高版本,您可以使用:C# ingTime==supportsDst){ 如果(!supportsDst)zoneInfo数组.Add(zoneInfo); 否则{ //有DST。查找DST的第一天并测试是否与发送的值匹配。天=年内的天偏移量 int foundDay=0; DateTime janDate=新的日期时间(2016,1,1,12,0,0);//正午 int startDay=zoneInfo.IsDaylightSavingTime(janDate)?180:0;//如果是南半球,则从一年中的180天开始 对于(int day=startDay;day,c#,asp.net,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc 3,对于Dot Net 3.5版及更高版本,您可以使用: TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow); 但对于低于3.5版的Dot Net,您可以通过以下方式手动处理: 首先,从客户端获取偏移量并将其存储在cookie中 function setTimezoneCookie(){ var timezone_cookie = "timezoneoffset"; // if the timezone cookie does
TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);
但对于低于3.5版的Dot Net,您可以通过以下方式手动处理:
首先,从客户端获取偏移量并将其存储在cookie中
function setTimezoneCookie(){
var timezone_cookie = "timezoneoffset";
// if the timezone cookie does not exist create one.
if (!$.cookie(timezone_cookie)) {
// check if the browser supports cookie
var test_cookie = 'test cookie';
$.cookie(test_cookie, true);
// browser supports cookie
if ($.cookie(test_cookie)) {
// delete the test cookie
$.cookie(test_cookie, null);
// create a new cookie
$.cookie(timezone_cookie, new Date().getTimezoneOffset());
// re-load the page
location.reload();
}
}
// if the current timezone and the one stored in cookie are different
// then store the new timezone in the cookie and refresh the page.
else {
var storedOffset = parseInt($.cookie(timezone_cookie));
var currentOffset = new Date().getTimezoneOffset();
// user may have changed the timezone
if (storedOffset !== currentOffset) {
$.cookie(timezone_cookie, new Date().getTimezoneOffset());
location.reload();
}
}
}
之后,您可以在后端代码中使用cookie,如下所示:
public static string ToClientTime(this DateTime dt)
{
// read the value from session
var timeOffSet = HttpContext.Current.Session["timezoneoffset"];
if (timeOffSet != null)
{
var offset = int.Parse(timeOffSet.ToString());
dt = dt.AddMinutes(-1 * offset);
return dt.ToString();
}
// if there is no offset in session return the datetime in server timezone
return dt.ToLocalTime().ToString();
}
您需要同时使用客户端和服务器端技术 在客户端:
(挑一个)
- 这适用于大多数现代浏览器:
Intl.DateTimeFormat().resolvedOptions().timeZone
- 对于较旧的浏览器,还有的
,或者jstz.determinate()
函数,尽管这些库通常只在较旧的应用程序中使用moment.tz.guess()
美国/纽约
。通过您喜欢的任何方式将结果发送到服务器
在服务器端:(挑一个)
- 使用
(仅限非Windows系统):TimeZoneInfo
TimeZoneInfo tzi=TimeZoneInfo.FindSystemTimeZoneById(“美国/纽约”);
- 使用(在任何操作系统上):
TimeZoneInfo tzi=TZConvert.GetTimeZoneInfo(“美国/纽约”);
- 使用(在任何操作系统上):
DateTimeZone tz=DateTimeZoneProviders.Tzdb[“美国/纽约”];
- 看看这个asp.net c解决方案
您可以从客户端到服务器(任何web API调用)获取此信息 借助
timezoneoffset
details,您可以实现同样的效果。在我的例子中,我将UTC日期时间转换为服务器端的客户端本地日期时间
DateTime clientDateTime = DateTime.UtcNow - new TimeSpan(timezoneOffset / 60, timezoneOffset % 60, 0);
相关帖子:当您不想使用ajax请求时,将时区放在cookie中是一个很好的选择,而且您不必使用JavaScript来显示日期,只需设置cookie即可。@RuudLenders但您需要JavaScript来设置cookie,因为这会提供与UTC的当前偏移量,而不是时区。请参阅中的“时区!=偏移量”。@MattJohnson:我知道,但这是我们能得到的最接近的。大多数情况下,我们使用时区信息来处理日期时间,我们只需要偏移量,而不是真正的时区。你能给我一个更好的解决方案吗?是的,看看我对这个问题的回答。谢谢你,迈克。我意识到你可以做
新的TimeSpan(0,timezoneOffset,0)
,它可以正确计算小时数。DST不重要。在调用“getTimezoneOffset()”时,JavaScript将考虑到这一点。重要的是,在服务器上,日期时间始终基于UTC,而在客户端,我们正确地传递了时区偏移量。关于DST,这很重要,但只有在您进行存储之类的操作时。对于观测DST的时区,白天偏移量与标准时间偏移量不同。因此,当您获得偏移量时,偏移量将是正确的,但如果您将其保留一段时间,则不会正确。这将为您提供与UTC的当前偏移量,而不是时区。请参阅中的“时区!=偏移量”。答案很好,但实际上您不应该这样做。您应该按原样检索数据(UTC)并应用偏移。您能提供此信息的参考吗?没有任何上下文的代码片段不是很有价值。如上所述,答案是无用的。
DateTime clientDate = TimeZoneInfo.ConvertTimeFromUtc(utcDate, timeZoneInfo);
// Time zone. Sets two form values:
// tzBaseUtcOffset: minutes from UTC (non-DST)
// tzDstDayOffset: number of days from 1/1/2016 until first day of DST ; 0 = no DST
var form = document.forms[0];
var janOffset = -new Date(2016, 0, 1).getTimezoneOffset(); // Jan
var julOffset = -new Date(2016, 6, 1).getTimezoneOffset(); // Jul
var baseUtcOffset = Math.min(janOffset, julOffset); // non DST offset (winter offset)
form.elements["tzBaseUtcOffset"].value = baseUtcOffset;
// Find first day of DST (from 1/1/2016)
var dstDayOffset = 0;
if (janOffset != julOffset) {
var startDay = janOffset > baseUtcOffset ? 180 : 0; // if southern hemisphere, start 180 days into year
for (var day = startDay; day < 365; day++) if (-new Date(2016, 0, day + 1, 12).getTimezoneOffset() > baseUtcOffset) { dstDayOffset = day; break; } // noon
}
form.elements["tzDstDayOffset"].value = dstDayOffset;
private TimeZoneInfo GetTimeZoneInfo(int baseUtcOffset, int dstDayOffset) {
// Converts client/browser data to TimeZoneInfo
// baseUtcOffset: minutes from UTC (non-DST)
// dstDayOffset: number of days from 1/1/2016 until first day of DST ; 0 = no DST
// Returns first zone info that matches input, or server zone if none found
List<TimeZoneInfo> zoneInfoArray = new List<TimeZoneInfo>(); // hold multiple matches
TimeSpan timeSpan = new TimeSpan(baseUtcOffset / 60, baseUtcOffset % 60, 0);
bool supportsDst = dstDayOffset != 0;
foreach (TimeZoneInfo zoneInfo in TimeZoneInfo.GetSystemTimeZones()) {
if (zoneInfo.BaseUtcOffset.Equals(timeSpan) && zoneInfo.SupportsDaylightSavingTime == supportsDst) {
if (!supportsDst) zoneInfoArray.Add(zoneInfo);
else {
// Has DST. Find first day of DST and test for match with sent value. Day = day offset into year
int foundDay = 0;
DateTime janDate = new DateTime(2016, 1, 1, 12, 0, 0); // noon
int startDay = zoneInfo.IsDaylightSavingTime(janDate) ? 180 : 0; // if southern hemsphere, start 180 days into year
for (int day = startDay; day < 365; day++) if (zoneInfo.IsDaylightSavingTime(janDate.AddDays(day))) { foundDay = day; break; }
if (foundDay == dstDayOffset) zoneInfoArray.Add(zoneInfo);
}
}
}
if (zoneInfoArray.Count == 0) return TimeZoneInfo.Local;
else return zoneInfoArray[0];
}
TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);
function setTimezoneCookie(){
var timezone_cookie = "timezoneoffset";
// if the timezone cookie does not exist create one.
if (!$.cookie(timezone_cookie)) {
// check if the browser supports cookie
var test_cookie = 'test cookie';
$.cookie(test_cookie, true);
// browser supports cookie
if ($.cookie(test_cookie)) {
// delete the test cookie
$.cookie(test_cookie, null);
// create a new cookie
$.cookie(timezone_cookie, new Date().getTimezoneOffset());
// re-load the page
location.reload();
}
}
// if the current timezone and the one stored in cookie are different
// then store the new timezone in the cookie and refresh the page.
else {
var storedOffset = parseInt($.cookie(timezone_cookie));
var currentOffset = new Date().getTimezoneOffset();
// user may have changed the timezone
if (storedOffset !== currentOffset) {
$.cookie(timezone_cookie, new Date().getTimezoneOffset());
location.reload();
}
}
public static string ToClientTime(this DateTime dt)
{
// read the value from session
var timeOffSet = HttpContext.Current.Session["timezoneoffset"];
if (timeOffSet != null)
{
var offset = int.Parse(timeOffSet.ToString());
dt = dt.AddMinutes(-1 * offset);
return dt.ToString();
}
// if there is no offset in session return the datetime in server timezone
return dt.ToLocalTime().ToString();
}
System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_TIMEZONE"] ;
TimeZoneInfo mytzone = TimeZoneInfo.Local;
var timezoneOffset = new Date().getTimezoneOffset();
DateTime clientDateTime = DateTime.UtcNow - new TimeSpan(timezoneOffset / 60, timezoneOffset % 60, 0);