gpt4 book ai didi

javascript - moment.js 在 firefox 中给出无效日期,但在 chrome 中没有

转载 作者:搜寻专家 更新时间:2023-11-01 04:54:10 25 4
gpt4 key购买 nike

我在使用 moment.js 时遇到了一个奇怪的问题。我写了一个函数将时间从 utc 格式转换为德国时间格式,在 chrome 中似乎一切正常。但是现在我在 firefox 上试过了,这里我得到了一个无效的日期。

moment.locale("de");

$('#from').datepicker({
format: "DD. MMMM YYYY"
});

$('#from').on('change',function() {

var a = moment($('#from').val(), "DD. MMMM YYYY").format("LLLL");
var b = moment(a).add(7,'days');


var localTime = moment.utc(b).toDate();
localTime = moment(localTime).format('DD. MMMM YYYY');


$('#to').val(localTime);

});



$('#to').datepicker({
format:'DD.MMMM YYYY'
});



$('#sendbtn').on('click',function(){

/...

var from = moment(fromfield.value).format();

var to = moment(tofield.value).format();


/...

$('#calendar').fullCalendar( 'gotoDate', from );
getEventDate(from,to,persons.value);


}

});

function getEventDate(start,end,people) {

var Calendar = $('#calendar');
Calendar.fullCalendar();
var Event = {
title:"Your stay for "+people+" people",
allDay: true,
start: start,
end: end

};
filljson(start,end,people);

Calendar.fullCalendar( 'renderEvent', Event );


}

/ ...

我看过这个answer但无法让它以任何一种方式工作。有人可以帮帮我吗?

最佳答案

从您的问题中不清楚是代码的哪一部分引发了错误,但可能的罪魁祸首是 Moment.js 只是将非 ISO-8601 字符串委托(delegate)给 Date.parse:https://github.com/moment/moment/issues/1407

因此,假设您使用 Moment 来解析用户输入或未知格式的另一个字段,或者解析非 ISO-8601 的格式,您将必须明确指定格式以确保交叉浏览器行为。否则,您将陷入变幻莫测的跨浏览器 Date.parse - 唯一始终如一的格式是 ISO-8601。

关于javascript - moment.js 在 firefox 中给出无效日期,但在 chrome 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27325750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com