Link to home
Start Free TrialLog in
Avatar of Shanmugam Rajagopal
Shanmugam Rajagopal

asked on

How to display browser time where DB server stores Pacific time values

Hi, I am using angular application. In the Db values are stored in pacific time and saved in string column; how do I display time based on browser timezone? I tried only at html side where it displays same value; not sure if value need to be retrieved from DB at server end and convert to pacific time and at browser UTC time. Please advise with example.
Avatar of HainKurt
HainKurt
Flag of Canada image

check this demo

https://jsfiddle.net/HainKurt/yc6v2s8z/
d = new Date();
d.setUTCFullYear(2017);
d.setUTCMonth(10);
d.setUTCDate(24);
d.setUTCHours(21);
d.setUTCMinutes(59);
d.setUTCSeconds(36);

console.log(d);
console.log(d.toLocaleString());
console.log(d.toLocaleDateString());
console.log(d.toLocaleTimeString());

Open in new window

it shows

Fri Nov 24 2017 16:59:36 GMT-0500 (Eastern Standard Time)
11/24/2017, 4:59:36 PM
11/24/2017
4:59:36 PM

Open in new window


it shows different values, because I am on EST time zone...
those values should come from your server...
or you can use

var d2 = new Date(Date.UTC(2017, 10, 24, 21, 59, 36, 0));

console.log(d2);
console.log(d2.toLocaleString());
console.log(d2.toLocaleDateString());
console.log(d2.toLocaleTimeString());

Open in new window


to get the same result as above...

https://jsfiddle.net/HainKurt/fLLn4cqq/

Date.UTC()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
Avatar of Shanmugam Rajagopal
Shanmugam Rajagopal

ASKER

HainKurt, Thanks for your response.

This is what I see in my side. I have the below angular function

 headerResource.GetLastLogin(function (data) {

                var mydate = Date(data.LAST_LOGIN_DATE_TIME);

}

data.LAST_LOGIN_DATE_TIME returns "27-OCT-2017 07:14:57" which is a string value and its pacific time. I used Date function to convert and mydate  returned "Fri Oct 27 2017 07:23:40 GMT-0700 (Pacific Daylight Time)". When I use

var year = mydate.getFullYear();
                var month = mydate.getMonth();
                var day = mydate.getDay();

It doesn't like and throws null reference error. Please advice how to convert to browser time.

Thanks for your help in advance.
sorry, confused a bit...
what is your value exactly that is coming from db?

27-OCT-2017 07:14:57
Fri Oct 27 2017 07:23:40 GMT-0700 (Pacific Daylight Time)

???
is there any way for you to format that one and get rid of names and use proper numbers like

DD/MM/YYYY HH:MI:SS TI (sample: 16/04/2017 10:24:32 AM)
or
YYYYMMDD HH24MISS (sample: 20170416 222432)
27-OCT-2017 07:14:57
Some improvement but UTC messed up time with -7 (testing in remote server located in West coast) but data in Pacific time. Now I added another property LAST_LOGIN_DATE_TIME1 by converting the LAST_LOGIN_DATE_TIME to DateTime at server side.

headerResource.GetLastLogin(function (data) {
                var dtemp = data.LAST_LOGIN_DATE_TIME1;
  //2017-10-27T10:48:12
               
         var d1 = new Date(dtemp);
                var y = d1.getFullYear();
                var m = d1.getMonth();
                var d = d1.getDate();
                var h = d1.getHours();
                var min = d1.getMinutes();
                var sec = d1.getSeconds();

                var utcDate = new Date(Date.UTC(y, m, d, h, min, sec));

                //Fri Oct 27 2017 03:48:12 GMT-0700 (Pacific Daylight Time)
                vm.LastLogin.LAST_LOGIN_DATE_TIME = $filter('date')(new Date(utcDate), 'yyyy-MM-dd HH:mm:ss');
              //2017-10-27 03:48:12
              }
you can use this

https://jsfiddle.net/HainKurt/8ha60a9k/

function strToDate(s) {
  var months = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "AUG", "SEP", "OCT", "NOV", "DEC"];
  var d = s.split(" ")[0];
  var t = s.split(" ")[1];

  var y = d.split("-")[2]
  var m = months.indexOf(d.split("-")[1]);
  var d = d.split("-")[0];

  var hh = t.split(":")[0]
  var mi = t.split(":")[1]
  var ss = t.split(":")[2]
  console.log("Date becomes: " + y + "-" + m + "-" + d + " " + hh + ":" + mi + ":" + ss);
  var date = new Date(Date.UTC(y, m, d, hh, mi, ss, 0));
  return date;
}

var myDate = '27-OCT-2017 07:14:57';
var d = strToDate(myDate);

>>> Wed Sep 27 2017 03:14:57 GMT-0400 (Eastern Daylight Time)

Open in new window

HainKurt, Thanks for your response. My question would be what if the people using from West coast; then again it will be -7 which becomes incorrect. (Currently I login to remote machine in west coast to test this)

Say for example my input was "30-OCT-2017 10:06:59" from the server last login time; after I call strToDate("30-OCT-2017 10:06:59") the result turend out to be "Sat Sep 30 2017 03:06:59 GMT-0700 (Pacific Daylight Time)" which is incorrect. The result suppose to be Sat Sep 30 2017 10:06:59 GMT-0700 (Pacific Daylight Time) when used in West Coast and Sat Sep 30 2017 07:06:59 GMT-0700 (Eastern Daylight Time) when used from East Coast.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.