0

My current timezone is in IST and I want it dynamic for example if I set my timezone to PST in my database then I need to set it to javascript and whenever I try to create date object it should return time in PST.

Without wasting much time in it I want to know is it possible or not if yes any possible way ?

I have this function anyways in time_zone_offset its returning time 5 hours ago than UTC and my system timezone is +5.5 than UTC date is just 2 mins. ago but its returning 10 hours ago

var currentDate = new Date(new Date().getTime()); // local system date
                var timezoneOffset = time_zone_offset;

                //Convert current dateTime into UTC dateTime
                var utcDate = new Date(currentDate.getTime() + (timezoneOffset * 60000));
                //console.log(utcDate);               

                //Convert date string (2015-02-02 07:12:13) in date object
                var t = date.split(/[- :]/);
                var today = new Date();
                // Apply each element to the Date function
                var date = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
                //date = new Date(date);
                var dateDiff        = Math.floor((utcDate.getTime()/1000)) - Math.floor((date.getTime()/1000));                  
                var formatedDate    = '';
                var time            = '';
                var fullDays        = Math.floor(dateDiff/(60*60*24));
                var fullHours       = Math.floor((dateDiff-(fullDays*60*60*24))/(60*60));
                var fullMinutes     = Math.floor((dateDiff-(fullDays*60*60*24)-(fullHours*60*60))/60);
                var fullSeconds     = Math.floor((dateDiff-(fullDays*60*60*24)-(fullHours*60*60)-(fullMinutes*60)));
                var dayArray        = new Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
                var monthArray      = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Nov','Dec');
                //console.log(dateDiff);

                date = new Date(date.getTime() - (timezoneOffset * 60000));

                if(fullDays > 2){
                    //var dt = new Date(date*1000);
                    time = monthArray[date.getMonth()]+' '+date.getDate()+' at '+formatAMPM(date);
                } else if(fullDays == 2){
                    time = '2 days';
                } else if(today.getDate() > t[2]){
                    time = 'Yesterday at '+formatAMPM(date);
                } else if(fullHours > 0) {
                    time = fullHours + ' hours';
                    if(fullHours == 1) {
                        time = fullHours + ' hour';
                    }
                } else if(fullMinutes > 0) {
                    time = fullMinutes + ' mins';
                    if (fullMinutes == 1) {
                        time = fullMinutes + ' min';
                    }
                } else {
                    time = 'Just now';
                }
                return time;
  • i don't think it is possible. You need to write your own methods to do the conversion. Whenever you create date object, it will return time in the timezone which is set in the local machine – Vigneswaran Marimuthu Apr 21 '15 at 05:10
  • You can work with UTC times instead, without an offset, and just add whatever you need to that. – adeneo Apr 21 '15 at 05:11
  • http://stackoverflow.com/questions/8805613/javascript-countdown-using-absolute-timezone the accepted answer here is probably what you want... – Norman Breau Apr 21 '15 at 05:11
  • Also, see "Time Zone != Offset" in [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info) – Matt Johnson-Pint Apr 21 '15 at 06:32

1 Answers1

1

I would use Moment.js and Moment Timezone if I were to deal a lot with client-site timezones. Plain JS dates are a pain.

It's a big lib so if it's just a one-time thing you want it's better not to use it and just send the timezone from the server and offset it with the client-site timezone of the user

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167