0

Without using moment.js, is there a way to construct a date in a specific timezone.

For example, I have two strings:

var date = '2018-01-23';
var time = '12:00';

I can construct the date like so:

var constructedDate = new Date(date.substring(0,4), date.substring(5,7)-1, date.substring(8,10), time.substring(0,2), time.substring(3,5));

which provides output of:

Tue Jan 23 12:00:00 GMT+00:00 2018

However, I am looking for output in a particular timezone (e.g +11:00)

Tue Jan 23 12:00:00 GMT+11:00 2018

Alternatively, is there a way to offset the date -11 hours so when I use the GMT date in my application it will be correct.

beano
  • 932
  • 1
  • 15
  • 28
  • I'd use Date.UTC : https://www.w3schools.com/jsref/jsref_utc.asp – olikaf Jan 19 '18 at 10:54
  • 1
    `new Date(…)` will always use UTC as basis. If you want to _create_ a date object that is actually 11 hours “off” of your given time value, then you need to add/subtract those 11 hours from it first. – CBroe Jan 19 '18 at 10:56
  • For the purpose of what I am trying to do, I think the easiest method is to add: var newDate = constructedDate.getTime() - (11*60*60*1000); – beano Jan 19 '18 at 11:09
  • "*Alternatively, is there a way to offset the date -11 hours so when I use the GMT date in my application it will be correct.*" Yes, just subtract 11 hours from the UTC time value, then use UTC methods to construct a formatted date. – RobG Jan 19 '18 at 21:56

0 Answers0