根据客户位置的日期时间

时间:2011-10-30 11:06:12

标签: c# javascript asp.net

我在数据库中有UTC的日期时间,现在我想根据用户的时区或用户的计算机显示该时间,就像用户 A 已登顶来自印度的问题一样然后,如果用户 A 转到美国,用户 A 可以根据印度查看提交日期然后根据 USA 显示,如果用户 B 在中国,那么他可以根据中国查看该问题。 我怎么能通过C#或javascript来做到这一点。 任何人都可以帮助我做到这一点。

2 个答案:

答案 0 :(得分:1)

您需要使用JavaScript从浏览器中收集必要的信息 - 对于此部分,请参阅http://www.pageloom.com/automatic-timezone-detection-with-javascript

获得此信息后,您可以设置TimeZone / TimeZoneInfo,然后可以使用这些信息来调整您的UTC DateTime值。

另一种更简单的选择是使用jQuery plugin called TimeAgo
有关详细信息,请参阅C# UTC to Users Local Time

答案 1 :(得分:0)

你可以这样做......使用javascript .....

此代码将以标准格式为您提供客户端时区偏移....

        <script type="text/javascript">
        // Original script by Josh Fraser (http://www.onlineaspect.com)
        // Some customization applied in this script code
        var minutes;
        function calculate_time_zone() {
            var rightNow = new Date();
            var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
            var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
            var temp = jan1.toGMTString();
            var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
            temp = june1.toGMTString();
            var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
            var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
            var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
            var dst;
            if (std_time_offset == daylight_time_offset) {
                dst = "0"; // daylight savings time is NOT observed
            } else {
                // positive is southern, negative is northern hemisphere
                var hemisphere = std_time_offset - daylight_time_offset;
                if (hemisphere >= 0)
                    std_time_offset = daylight_time_offset;
                dst = "1"; // daylight savings time is observed
            }
            var i;
        // Here set the value of hidden field to the ClientTimeZone.
                         minutes = convert(std_time_offset);
            TimeField = document.getElementById("HiddenFieldClientTime");
            TimeField.value = minutes;
            alert('your time zone is ' + minutes);      
        }
        // This function is to convert the timezoneoffset to Standard format
        function convert(value) {
            var hours = parseInt(value);
            value -= parseInt(value);
            value *= 60;
            var mins = parseInt(value);
            value -= parseInt(value);
            value *= 60;
            var secs = parseInt(value);
            var display_hours = hours;
            // handle GMT case (00:00)
            if (hours == 0) {
                display_hours = "00";
            } else if (hours > 0) {
                // add a plus sign and perhaps an extra 0
                display_hours = (hours < 10) ? "+0" + hours : "+" + hours;
            } else {
                // add an extra 0 if needed
                display_hours = (hours > -10) ? "-0" + Math.abs(hours) : hours;
            }
            mins = (mins < 10) ? "0" + mins : mins;
            return display_hours + ":" + mins;
        }
        // Adding the function to onload event of document object
        onload = calculate_time_zone;
</script>

我建议你通过this link

并看一下这个time detection