如何从 UTC 日期格式获取正确的时区(服务器时区)?

发布于 2024-09-15 16:54:57 字数 410 浏览 8 评论 0原文

作为以下链接的后续内容。

如何使用 javascript 或 jquery 将此日期类型 2010-06-24T00:00:00Z 格式化为 sun,24/06/10 7.15 pm (CDT)

我正在将 utc 日期格式转换为简单日期格式:

2010-06-24T00:00:00Z 到 sun,24/06/10 7.15 pm (CDT)(转换时间)

但是有没有办法识别有效时区,因为上述转换始终检测基于时区在客户端机器上。还是我们无法检测到 UTC 的有效时区?

As follow up of the below link.

How to format this date type 2010-06-24T00:00:00Z to sun,24/06/10 7.15 p.m (CDT) using javascript or jquery

I'm converting the utc date format to simple date format:

2010-06-24T00:00:00Z to sun,24/06/10 7.15 p.m (CDT) (converted time)

But is there a way to identify the valid timezone, as the above conversion always detects the time zone based on client machine. Or is it that we can't detect the valid time zone from UTC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一向肩并 2024-09-22 16:54:57

UTC 没有时区,它是“通用的”,因为无论你在哪里,它总是相同的。时区信息必须由观察者确定。

UTC has no time zone, it is "universal" because it is always the same no matter where you are. Time zone information has to be determined by the observer.

围归者 2024-09-22 16:54:57

您没有定义“有效时区”的含义 - 您的意思是本地时区吗? (毕竟 UTC 是一个完全有效的时区,或者至少如果您位于西欧时区且不是英国夏令时间的话……)

如果您有 UTC 日期并且想将其转换为日期在当地时区,这很简单:

function UTCtoLocal(inDate) {
    // inDate can be null, a string or an object
    // if it's an object, it can be a date or event

    var localDate = new Date();
    var utcDate = new Date().toUTCString();

    if (inDate) {
        if (typeof inDate == "string") {
            utcDate = inDate;
        }
        else { 
            if (inDate.getDay) { // is it really a date?
                utcDate = inDate.toString();
            }
        }
    }

    utcDate = utcDate.substr(0, utcDate.length-3);
    localDate.setTime(Date.parse(utcDate));
    return localDate;
}

You didn't define what you mean by "valid timezone" -- do you mean local timezone? (UTC is a perfectly valid timezone, after all--or at least it is if you're in the Western European Time Zone and it's not British Summer Time…)

If you have a UTC date and you want to turn it into a date in the local timezone, that's straightforward:

function UTCtoLocal(inDate) {
    // inDate can be null, a string or an object
    // if it's an object, it can be a date or event

    var localDate = new Date();
    var utcDate = new Date().toUTCString();

    if (inDate) {
        if (typeof inDate == "string") {
            utcDate = inDate;
        }
        else { 
            if (inDate.getDay) { // is it really a date?
                utcDate = inDate.toString();
            }
        }
    }

    utcDate = utcDate.substr(0, utcDate.length-3);
    localDate.setTime(Date.parse(utcDate));
    return localDate;
}
蓝礼 2024-09-22 16:54:57

您可以在 C# 中使用以下代码通过传递时区值来获取日期(例如:+08:00:00)

Console.WriteLine("Enter the Time zone: ");
            string zoneInfo = Console.ReadLine();
            int Flags = 0;
            ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
            string myTime = "";
            string displayName = "";
            foreach (var timeZoneInfo in zones)
            {
                if (timeZoneInfo.BaseUtcOffset`enter code here`.ToString().Equals(zoneInfo))
                {
                    string timeZoneId = timeZoneInfo.Id;`enter code here`
                    myTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId)).ToString();
                    displayName = timeZoneInfo.DaylightName.ToString();
                    break;
                }
            }
            Console.WriteLine(displayName + ":" + myTime);
            Console.ReadLine();

You can use below code in C# to get the date by passing the timezone value (e.g.: +08:00:00)

Console.WriteLine("Enter the Time zone: ");
            string zoneInfo = Console.ReadLine();
            int Flags = 0;
            ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
            string myTime = "";
            string displayName = "";
            foreach (var timeZoneInfo in zones)
            {
                if (timeZoneInfo.BaseUtcOffset`enter code here`.ToString().Equals(zoneInfo))
                {
                    string timeZoneId = timeZoneInfo.Id;`enter code here`
                    myTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId)).ToString();
                    displayName = timeZoneInfo.DaylightName.ToString();
                    break;
                }
            }
            Console.WriteLine(displayName + ":" + myTime);
            Console.ReadLine();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文