1

I'm using Moment and Moment Timezone.

I have a unix timestamp which I then want to add just the timezone offset to, but the offset gets added to the time as well- I don't want this:

var testDateTimeUnix = 1438352400; 
// Fri, 31 Jul 2015 14:20:00 
var testDateTimeUnixFormatted = moment(testDateTimeUnix, 'X').utc().format() 
// "2015-07-31T14:20:00+00:00"
var testDateTimeWithTimezoneOffset = moment(testDateTimeUnixFormatted).utc().tz('Europe/Bucharest').format(); 
// “2015-07-31T17:20:00+03:00"

The formatted date I want is:

"2015-07-31T14:20:00+03:00"

Any help would be really appreciated.

J_P
  • 599
  • 1
  • 9
  • 20
  • duplicate of http://stackoverflow.com/questions/15347589/moment-js-format-date-in-a-specific-timezone ? – Astrogat Aug 06 '15 at 10:16

1 Answers1

0

I am not sure why you want to do that... When showing 17:20:00+03:00 it means that the 3 hours offset is included in the time. This means that I know of no method in moment or moment-timezone that can help since they will always apply the offset to the moment.

There is one hacky way, very dirty and I would not recommend it for production code since it relies on internals and may well break other things in the library (you have been duly warned :) )

var moment = require( 'moment-timezone' ),

// your timestamp
tx = 1438352400;
// expressed as a moment in UTC
txu = moment( tx, 'X').utc();
// now get the UTC offset for Bucharest (in minutes) 
txo = moment().tz('Europe/Bucharest').utcOffset();

// This is the dirty part and we hack the moment's internal structure. Bad.
// Set the offset to Bucharest's
txu._offset = txo; 

// and display
console.log( txu.format() );
// 2015-07-31T14:20:00+03:00

```

So it's doable but I don't know what the time the final value would represent. There is a reason why Moment offsets the time.

tgo
  • 1,515
  • 7
  • 11