检查ISO 8601字符串是否在UTC中

发布于 2025-02-13 18:51:15 字数 825 浏览 1 评论 0原文

我想要一个打字稿功能,该功能检查ISO 8601 DateTime字符串是否在UTC中。因此,输出应该是:

isUTC('2020-01-01T00:00:00Z')      // true
isUTC('2020-01-01T00:00:00+00')    // true
isUTC('2020-01-01T00:00:00+0000)   // true
isUTC('2020-01-01T00:00:00+00:00') // true
isUTC('2020-01-01T00:00:00+01)     // false
isUTC('2020-01-01T00:00:00+0100)   // false
isUTC('2020-01-01T00:00:00+01:00') // false

如果可能的话,我想避免正则表达式并使用现有的解析器或分解器。理想情况下,将有一个分解ISO 8601字符串的库,因此我可以检查iso8601decomposer(value).TimeZoneOffSet === 0。类似的内容是否存在于Typescript/JavaScript中?

我发现一些答案​​检查value ===新date(date.parse(value))。toisostring(),但这对于上面的第2行和第3行不起作用。

I want a TypeScript function that checks if an ISO 8601 datetime string is in UTC. So the output should be:

isUTC('2020-01-01T00:00:00Z')      // true
isUTC('2020-01-01T00:00:00+00')    // true
isUTC('2020-01-01T00:00:00+0000)   // true
isUTC('2020-01-01T00:00:00+00:00') // true
isUTC('2020-01-01T00:00:00+01)     // false
isUTC('2020-01-01T00:00:00+0100)   // false
isUTC('2020-01-01T00:00:00+01:00') // false

If possible I would like to avoid regular expressions and use an existing parser or decomposer instead. Ideally there would be a library that decomposes ISO 8601 strings, so that I can check Iso8601Decomposer(value).timezoneOffset === 0. Does something like this exist in TypeScript/JavaScript?

I found some answers that were checking if value === new Date(Date.parse(value)).toISOString(), but this does not work for lines 2 and 3 above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2025-02-20 18:51:15

您可以使用 luxon库ISO日期字符串。

然后,可以在几分钟内获得UTC偏移,如果是0,我们在UTC(或GMT)中,

我将所有这些都包装在必需的iSUTC()函数中。

let { DateTime } = luxon;

let inputs = [
    '2020-01-01T00:00:00Z',
    '2020-01-01T00:00:00+00',
    '2020-01-01T00:00:00+0000',
    '2020-01-01T00:00:00+00:00',
    '2020-01-01T00:00:00+01',
    '2020-01-01T00:00:00+0100',
    '2020-01-01T00:00:00+01:00'
];


function isUTC(input) {
    const dt = DateTime.fromISO(input, { setZone: true })
    return dt.offset === 0;
}

console.log('Input'.padEnd(30), 'isUTC');
for(let input of inputs) {
    console.log(input.padEnd(30), isUTC(input));
}
.as-console-wrapper { max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.1/luxon.min.js" integrity="sha512-Nw0Abk+Ywwk5FzYTxtB70/xJRiCI0S2ORbXI3VBlFpKJ44LM6cW2WxIIolyKEOxOuMI90GIfXdlZRJepu7cczA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

can 也可以使用正则态度来执行此操作:

let inputs = [
    '2020-01-01T00:00:00Z',
    '2020-01-01T00:00:00+00',
    '2020-01-01T00:00:00+0000',
    '2020-01-01T00:00:00+00:00',
    '2020-01-01T00:00:00+01',
    '2020-01-01T00:00:00+0100',
    '2020-01-01T00:00:00+01:00'
];


function isUTC(input) {
    return getUTCOffsetMinutes(input) === 0;
}

function getUTCOffsetMinutes(isoDate) {
    // The pattern will be ±[hh]:[mm], ±[hh][mm], or ±[hh], or 'Z'
    const offsetPattern = /([+-]\d{2}|Z):?(\d{2})?\s*$/;
    if (!offsetPattern.test(isoDate)) {
        throw new Error("Cannot parse UTC offset.")
    }
    const result = offsetPattern.exec(isoDate);
    return (+result[1] || 0) * 60 + (+result[2] || 0);
}

console.log('Input'.padEnd(30), 'isUTC');
for(let input of inputs) {
    console.log(input.padEnd(30), isUTC(input));
}
.as-console-wrapper { max-height: 100% !important; }

You can use the luxon library to parse ISO date strings.

One can then get the UTC offset in minutes, if this is 0 we're in UTC (or GMT)

I've wrapped this all up in the required isUTC() function.

let { DateTime } = luxon;

let inputs = [
    '2020-01-01T00:00:00Z',
    '2020-01-01T00:00:00+00',
    '2020-01-01T00:00:00+0000',
    '2020-01-01T00:00:00+00:00',
    '2020-01-01T00:00:00+01',
    '2020-01-01T00:00:00+0100',
    '2020-01-01T00:00:00+01:00'
];


function isUTC(input) {
    const dt = DateTime.fromISO(input, { setZone: true })
    return dt.offset === 0;
}

console.log('Input'.padEnd(30), 'isUTC');
for(let input of inputs) {
    console.log(input.padEnd(30), isUTC(input));
}
.as-console-wrapper { max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.1/luxon.min.js" integrity="sha512-Nw0Abk+Ywwk5FzYTxtB70/xJRiCI0S2ORbXI3VBlFpKJ44LM6cW2WxIIolyKEOxOuMI90GIfXdlZRJepu7cczA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

You can do this with RegEx too:

let inputs = [
    '2020-01-01T00:00:00Z',
    '2020-01-01T00:00:00+00',
    '2020-01-01T00:00:00+0000',
    '2020-01-01T00:00:00+00:00',
    '2020-01-01T00:00:00+01',
    '2020-01-01T00:00:00+0100',
    '2020-01-01T00:00:00+01:00'
];


function isUTC(input) {
    return getUTCOffsetMinutes(input) === 0;
}

function getUTCOffsetMinutes(isoDate) {
    // The pattern will be ±[hh]:[mm], ±[hh][mm], or ±[hh], or 'Z'
    const offsetPattern = /([+-]\d{2}|Z):?(\d{2})?\s*$/;
    if (!offsetPattern.test(isoDate)) {
        throw new Error("Cannot parse UTC offset.")
    }
    const result = offsetPattern.exec(isoDate);
    return (+result[1] || 0) * 60 + (+result[2] || 0);
}

console.log('Input'.padEnd(30), 'isUTC');
for(let input of inputs) {
    console.log(input.padEnd(30), isUTC(input));
}
.as-console-wrapper { max-height: 100% !important; }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文