我创建了自己的班级
PrecisionDate
它存储一个与日期相同的毫秒数,但以微秒计的小数部分。它使用
new Date()
精确到毫秒。然后,我使用regex和capture组来获取微秒。
const ISO8601_REGEX = /^(\d{4})-(0\d|1[0-2])-([0-2]\d|3[0-1])T([01]\d|2[0-4]):([0-5]\d):([0-5]\d)[.,](\d{3})?(\d{3})?(\d+)?\+(\d\d):(\d\d)/;
public static parse(timestamp: string): number {
// Get the full time except for the microseconds
let time = Date.parse(timestamp);
const captureGroups = ISO8601_REGEX.exec(timestamp);
const microSeconds = captureGroups[ISO8601_REGEX_MICRO];
if (microSeconds) {
time += parseInt(microSeconds, 10) / 1000;
}
return time;
}