MomentJS 将本地化日期转换为 UTC

MomentJS convert localized date to UTC

我目前有以下德语格式的日期字符串:

01.09.2021, 14:28:53 // which means the 1st of September, 2021

我尝试将其解析为 UTC,但不幸的是,当我这样做时,它会切换日期和月份。

2021-01-09T14:28:53.000Z

我拥有的代码:

const date: string = '01.09.2021, 14:28:53';
moment.utc(date).locale('de').toISOString();

我努力实现的是:

2021-09-01T14:28:53.000Z

传递您的自定义格式

const date = '01.09.2021, 14:28:53';
const tmp = moment(date, 'DD.MM.YYYY, HH:mm:ss').locale('de').toISOString();
console.log(tmp);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

暂时不用

const de = true; // as opposed to en = true;
const str = `01.09.2021, 14:28:53`
const [_,aa,bb,yyyy,t] = str.split(/(\d{2})\.(\d{2})\.(\d{4}), (\d{2}\:\d{2}\:\d{2})/)
dd = de ? aa : bb;
mm = de ? bb : aa;
console.log(dd,mm,yyyy,t);
console.log(`${yyyy}-${mm}-${dd}T${t}.000Z`);