Askiitians
Askiitians

Reputation: 291

Datetime according to Client location

I have The Date time in UTC in database, now I want to show that time according to the User's Timezone or user's computer machine, like if user A has summit a question from India then User A can see the Submitted date according to India, if user A goes to USA then it shows according to USA, and if User B is in China then He can view that question according to China. how can I do that via C# or javascript. any one can help me to do that.

Upvotes: 0

Views: 427

Answers (2)

Glory Raj
Glory Raj

Reputation: 17701

you can do like this.. by using javascript.....

This code will give you client time zone offset in standard format....

        <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>

I recommended you pls go through this link

and take a look at this one also time detection

Upvotes: 0

Yahia
Yahia

Reputation: 70369

You will need to use JavaScript to gather the necessary information from the browser - for this part see http://www.pageloom.com/automatic-timezone-detection-with-javascript

When you have this information you can setup a TimeZone / TimeZoneInfo which in turn can be used to adjust your UTC DateTime values.

Another easier option is using a jQuery plugin called TimeAgo.
For details see C# UTC to Users Local Time

Upvotes: 1

Related Questions