1

I have a simple php script that returns time in a following format:

date_default_timezone_set('Asia/Tokyo');
echo (new DateTime())->format('F d, Y H:i:s O');

the output of it is:

October 01, 2015 18:34:35 +0900

Now I fetch this data in my jquery script and I want to create a new Date() object from this string. However, I don't know how to include the timezone there. So far I'm doing like this:

var serverTime = "October 01, 2015 18:34:35 +0900";//fetched from server
var timezone = serverTime.substr(serverTime.length - 5);
var substractedDate = serverTime.substr(0, serverTime.length - 5);
var tokyoTime = new Date(substractedDate);
alert(tokyoTime);

but it shows me:

Thu Oct 01 2015 18:40:34 GMT+0200 (Central Europe Daylight Time)

How can I create a date like this:

Thu Oct 01 2015 18:40:34 GMT+0900 (Asian...)

?

user3766930
  • 5,629
  • 10
  • 51
  • 104
  • Could it be because you alert the romeTime instead of the tokyoTime? – Denny Mueller Oct 01 '15 at 09:46
  • oh yeah sorry, I forgot to rename the variables for putting it here, now I fixed it here, the problem appears in tokyoTime of course :) – user3766930 Oct 01 '15 at 10:01
  • Cant test it since I dont have any php environment available. Have you tried if it behaves different using the same Class? Because you use in the First example `DateTime` and in the second one `Date`. You also could check if `date_default_timezone_get();` is correctly set right before you set tokyoTime. – Denny Mueller Oct 01 '15 at 10:15
  • Thanks, I assume the php works fine since it returns me `October 01, 2015 18:34:35 +0900`, the real question is how to include the timezone in creation of the date object in jquery... – user3766930 Oct 01 '15 at 10:48
  • Have you tried this plugin? http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript – Zim84 Oct 01 '15 at 12:13
  • @Zim84 I tried, but I need a date object later on and I cannot convert moment to date.. – user3766930 Oct 01 '15 at 12:16
  • 1
    Next one: http://stackoverflow.com/questions/85116/how-do-i-display-a-date-time-in-the-users-locale-format-and-time-offset – Zim84 Oct 01 '15 at 12:27

1 Answers1

0

use this code and customize your getDate function by date_default_timezone_get() to get server time zone:

var serverTime = "<?php echo (new DateTime())->format('F d, Y H:i:s O'); ?>";

or use TimezoneJS plugin: Download TimezoneJS from.

And use it:

<?php date_default_timezone_set('Asia/Tokyo'); ?>
var dt = new timezoneJS.Date('<?php echo ?>', '<?php echo date_default_timezone_get();?>');