I'm trying to remove the javascript Date offset, so that the local time a date was recorded in from a given country can be displayed.
I have a test that only works when the timezone is GMT
describe('.removeLocalDateOffset()', () => {
it('removes the timezone offset', () => {
const input = '2017-10-03T08:53:12.000-07:00';
const result = removeLocalDateOffset(input);
expect(result.toISOString()).toBe('2017-10-03T08:53:12.000Z');
});
});
And the function I want to do the job:
const removeLocalDateOffset = (date) => {
const originalDate = new Date(date);
return new Date(Date.UTC(
originalDate.getFullYear(),
originalDate.getMonth(),
originalDate.getDate(),
originalDate.getHours(),
originalDate.getMinutes(),
originalDate.getSeconds(),
));
}
Any suggestions for how I can do this using the methods of the new Date()
object? Ideally not doing a .slice()
on a substring.
Test Results. I am currently in GMT:
Expected: "2017-10-03T08:53:12.000Z"
Received: "2017-10-03T16:53:12.000Z"