如果使用库是合理的(在其他日期操作的情况下),Luxon是一个很好的选择:
https://moment.github.io/luxon/
import { DateTime } from 'luxon';
// Creates a DateTime instance from an ISO 8601-compliant string
const date = DateTime.fromISO('2019-01-03T07:36:40Z');
// Format date to ISO 8601 string
// -> '2019-01-03T08:36:40.000+01:00'
date.toISO();
// Set format to 'basic'
// -> '20190103T083640.000+0100'
date.toISO({format: 'basic'});
// We don't need milliseconds
// -> '20190103T083640+0100'
date.toISO({format: 'basic', suppressMilliseconds: true});
// My system uses a time zone with an offset of +01:00, which is used by Luxon as default.
// So I have to convert the date to UTC (offset +00:00).
// -> '20190103T073640Z'
date.toUTC(0).toISO({format: 'basic', suppressMilliseconds: true});
// As an one-liner
DateTime
.fromISO('2019-01-03T07:36:40Z')
.toUTC(0)
.toISO({ format: 'basic', suppressMilliseconds: true })