Skip to content

Instantly share code, notes, and snippets.

@tomoima525
Created June 10, 2019 16:53
Show Gist options
  • Save tomoima525/f71344a68c77aeae1e062e08e86dc46c to your computer and use it in GitHub Desktop.
Save tomoima525/f71344a68c77aeae1e062e08e86dc46c to your computer and use it in GitHub Desktop.
format to utc date
import formatDateToUTC from '../utils/format_date_to_utc';
describe('formatDateToUTC', () => {
test('should format date to UTC time zone', () => {
const date = new Date('May 9, 2019 11:33:30 GMT-07:00'); // Pacific Day Light Time
expect(formatDateToUTC(date)).toEqual('201905091833');
});
});
const formatDateToUTC = date => {
const month = `${date.getUTCMonth() + 1}`;
const day = `${date.getUTCDate()}`;
const hour = `${date.getUTCHours()}`;
const min = `${date.getUTCMinutes()}`;
const year = `${date.getFullYear()}`;
const list = [year, month, day, hour, min];
return list
.map(v => {
if (v.length === 1) {
return `0${v}`;
}
return v;
})
.join('');
};
module.exports = formatDateToUTC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment