momentJS : How to convert UTC to BST and then subtraction (or addition) of 1 hr on the resulting BST time


#1
moment.utc('2016-05-20 10:16:00').toDate()
Fri May 20 2016 11:16:00 GMT+0100 (GMT Daylight Time)

converts to local time from an UTC input, and afterward I would like to subtract one hour with:

moment.utc('2016-05-20 10:16:00').subtract(1, 'hours').format("YYYY-MM-DD HH:mm:ss")

And I expect to get

 '2016-05-20 10:16:00'

But why am I getting:

 '2016-05-20 09:16:00'

I think this is related to the moment object being mutable and that subtract or add will modify the original date so that it is executed twice, but already tried the clone() but not sure why still not working…

Thanks


#2

Just guessing…

moment.utc('2016-05-20 10:16:00').subtract(1, 'hours').toDate().format("YYYY-MM-DD HH:mm:ss")

#3
moment.utc('2016-05-20 10:16:00').subtract(1, 'hours').toDate()
Fri May 20 2016 10:16:00 GMT+0100 (GMT Daylight Time)

Gets the required date but .format(“YYYY-MM-DD HH:mm:ss”) does not work, if trying to format it or if I tried:

moment.utc('2016-05-20 10:16:00').subtract(1, 'hours').format("YYYY-MM-DD HH:mm:ss")
'2016-05-20 09:16:00'

I get the conversion but the incorrect information

and doing what you suggested Rob:

moment.utc('2016-05-20 10:16:00').subtract(1, 'hours').toDate().format("YYYY-MM-DD HH:mm:ss")

TypeError: moment.utc(...).subtract(...).toDate(...).format is not a function

Or do I do the addition/subtraction first before the conversion?

Also, current local UK time is 09:52, and why is

moment.utc().toDate()
Mon May 23 2016 09:52:38 GMT+0100 (GMT Daylight Time)

moment().toDate()
Mon May 23 2016 09:52:55 GMT+0100 (GMT Daylight Time)

Both returning the same time?