0

EDIT : Btw, I have no idea why this question was marked as a duplicate. The answers in the original question does not work for me. i.e, getting wrong results and stuffs. Furthermore, none of the answers deal with phstc's dateFormat function. Do correct me if I'm wrong. Btw, I have solved this question. Do take a look at my answer.

I want to change a UTC datetime to my browser's timezone. I'm using phstc's dateFormat in pure javascript form. Let's say I convert a datetime of 2014-06-27 07:11:16 using a javascript Date() function. The result I got was

Fri Jun 27 2014 07:11:16 GMT+0800 (Malay Peninsula Standard Time)

Then when I use phstc's toBrowserTimeZone function, it still returns me the same datetime. I wanted to get something like 2014-06-27 15:11:16

Here is the code below:

var originalDateTime = new Date(`2014-06-27 07:11:16`);
alert(DateFormat.format.toBrowserTimeZone(originalDateTime,"yyyy/MM/dd HH:mm:ss"));
John Evans Solachuk
  • 1,953
  • 5
  • 31
  • 67

1 Answers1

0

According to this statement in phstc's dateFormat page,

value = String representing date in ISO time (ā€œ2013-09-14T23:22:33Zā€) or String representing 
default JAXB formatting of java.util.Date (ā€œ2013-09-14T16:22:33.527-07:00ā€) or String representing 
Unix Timestamp (Sat Sep 14 2013 16:22:33 GMT-0700 (PDT)) or javascript date object.

JS Date object should work but unfortunately, it didn't. Well, I got it fixed by changing the datetime to other formats stated above first before calling the toBrowserTimeZone() function. For example,

 var originalDateTime = DateFormat.format.date('2014-06-27 07:11:16',"yyyy-MM-ddTHH:mm:ssZ");
 var newDateTime = DateFormat.format.toBrowserTimeZone(originalDateTime);
John Evans Solachuk
  • 1,953
  • 5
  • 31
  • 67