const epoch = Date.UTC(2017,09,24)
const moment_epoch = moment.utc(epoch).utcOffset(5.5).valueOf()
Why do epoch and moment_epoch produce the same output when I am adding the offset?
How do I get the epoch time after adding the offset?
const epoch = Date.UTC(2017,09,24)
const moment_epoch = moment.utc(epoch).utcOffset(5.5).valueOf()
Why do epoch and moment_epoch produce the same output when I am adding the offset?
How do I get the epoch time after adding the offset?
If you check the values, step by step, it becomes easier to understand what's happening:
Date.UTC(2017,09,24) produces the timestamp equivalent to October 24th 2017, at midnight, in UTC (1508803200000).moment.utc(epoch) produces a date in UTC, equivalent to the millis value above:
Tue Oct 24 2017 00:00:00 GMT+0000
moment.utc(epoch).utcOffset(5.5) just converts the UTC date to +05:30 offset:
Tue Oct 24 2017 05:30:00 GMT+0530
But both dates correspond to the same instant (midnight in UTC is the same as 5:30 AM in +05:30 offset), so the millis value will be the same (both will be 1508803200000).
Not sure exactly what you're trying to do, but anyway:
if you want to add 5 hours and 30 minutes to the date, just do:
moment.utc(epoch).add(5, 'h').add(30, 'm')
Note that this doesn't change the offset. It just gets a date which is 5 hours and 30 minutes after the original one.
if you want a date that correponds to midnight at +05:30 offset, you can do:
moment.utc(epoch).utcOffset(5.5, true)
The second parameter tells momentjs to preserve the same local date/time, so instead of converting to date/time to the offset, it keeps the same values - in this case, it'll be midnight in +05:30 offset:
Tue Oct 24 2017 00:00:00 GMT+0530
Regarding your jsfiddle link, I've just changed it a little bit to also print the offset:
moment.utc(1508803200000).format("DD/MM/YYYY HH:mm Z") // 24/10/2017 00:00 +00:00
moment.utc(1508803200000).utcOffset(5.5).format("DD/MM/YYYY HH:mm Z") // 24/10/2017 05:30 +05:30
moment.utc(1508803200000).utcOffset(5.5, true).format("DD/MM/YYYY HH:mm Z") // 24/10/2017 00:00 +05:30
moment.utc(1508803200000).valueOf() // 1508803200000
moment.utc(1508803200000).utcOffset(5.5).valueOf() // 1508803200000
moment.utc(1508803200000).utcOffset(5.5, true).valueOf() // 1508783400000
The value 1508803200000 corresponds to both 2017-10-24T00:00:00Z (midnight in UTC) and 2017-10-24T05:30+05:30 (5:30 AM in India).
The first line just gets this value in UTC, so the result is 24/10/2017 00:00 +00:00.
The second line converts this same value to the corresponding local date/time in +05:30 offset. This doesn't change the epoch value (it still corresponds to 1508803200000). The result is the equivalent date/time in India, which is 24/10/2017 05:30 +05:30 (5:30 AM in offset +05:30, the equivalent of midnight in UTC - both represent the same instant, that's why the valueOf() is the same).
The third line (with utcOffset(5.5, true)) doesn't convert midnight in UTC to 5:30 AM in India. Instead, it creates a date equivalent to midnight in India, that's why the result is 24/10/2017 00:00 +05:30. And the valueOf() returns 1508783400000, which is equivalent to both midnight in India and 23/10/2017 18:30 in UTC (6:30 PM of the day before, that's why the value is lower than the original one).