-2

I have all the date components of a new Date() function in javascript.

new Date(yyyy,mm,dd,hours,minutes,seconds,milliseconds);

The date components are obtained from a Malaysian server using jsp. I am not in Malaysia so when I do new Date(yyyy,mm,dd,hours,minutes,seconds,milliseconds); it provides me the time in my local area. How can I get it in Malaysian time zone?

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
suman
  • 195
  • 13
  • Javascript is client side.... – Idris Jul 05 '14 at 17:33
  • @Idris: it can also be server-side with node.js... – djikay Jul 05 '14 at 17:35
  • @Idris - Doesn't have to be. You can run JS server-side as well. Though in this case I think you're right. – PM 77-1 Jul 05 '14 at 17:36
  • Well it was never stated, or tagged. So I'd assume its just plain client side JS. Guess we'll just have to wait for OP to confirm @djikay – Idris Jul 05 '14 at 17:36
  • @Idris: I agree, I just wanted to mention that, in general, it doesn't *have* to be client-side, but in this case you're probably right. – djikay Jul 05 '14 at 17:37

2 Answers2

0

I think without a library you won't have a good time doing such things, i.e. it's not supported and Date is only aware of local time. I would suggest having a look at the timezone module of momentjs: http://momentjs.com/timezone/.

PermaFrost
  • 1,386
  • 12
  • 10
0

In JSP, you can use JSTL Formatting Tags - Internationalization that provides a set of tags for parsing and formatting locale-sensitive numbers and dates.

The timeZone tag establishes the time zone (specified with the value attribute) to be used by any nested formatDate tags.

Sample code: (Change the timezone as per your need)

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<jsp:useBean id="now" class="java.util.Date" />
<c:set var="timeZone" value="GMT-8"/>

Date in the current time zone:
<fmt:formatDate value="${today}" type="both" /><br/>

Date in the GMT-8 time zone:
<fmt:timeZone value="${timeZone}">
      <fmt:formatDate value="${today}" timeZone="${timeZone}" type="both" />
</fmt:timeZone>

Find complete sample code here and here

Braj
  • 46,415
  • 5
  • 60
  • 76