https://fullcalendar.io/选择事件使用答案中的代码保存开始时间如何将 tstzrange 传递给 postgres
JavaScript:
document.addEventListener('DOMContentLoaded', function() {
const calOpts = {
locale: 'ee',
slotDuration: '00:15:00',
slotLabelInterval: '00:15',
allDaySlot: false,
selectConstraint: 'businessHours',
select : function( selectionInfo ) {
document.getElementById('start').value = selectionInfo.start
},
selectable: true,
events: { url: 'api/fullcalendar' }
}
const calendarEl = document.getElementById('calendar')
calendar = new FullCalendar.Calendar(calendarEl, calOpts)
calendar.render()
})
HTML:
<form action="~/kinnitatud" method="post">
<input type="hidden" name="start" id="start" />
<input type='submit'/>
</form>
SelectionInfo.start 包含本地化值,例如
2024 年 12 月 30 日星期一 09:30:00 GMT+0200(Ida-Euroopa 标准)
.NET 9 MVC 控制器使用以下方法解析它
[HttpPost]
public async Task<IActionResult> Kinnitatud(string start) {
DateTime algus = DateTimeOffset.Parse(start,new CultureInfo("ee")).UtcDateTime;
}
DateTimeOffset.Parse 引发异常:
String 'Mon Dec 30 2024 09:30:00 GMT+0200 (Ida-Euroopa standardaeg)' was not recognized as a valid DateTime.
也尝试过没有
语言环境:'ee',
设置但起始值仍为 ee 文化。ee 是浏览器文化。Fullcalendar 始终以浏览器文化格式返回 choiceInfo.start。
尝试在控制器中使用 DateTime.Parse:
DateTime algus = DateTime.Parse(start, new CultureInfo("ee")).ToUniversalTime()
例外情况也一样。
如何强制 fullcalendar 以不变的文化格式返回选择的开始时间?
或者:用户的浏览器文化不同。如何解析fullcalendar返回的startselectionInfo.start?
你说过
...但事实并非如此。根据文档, Fullcalendar在字段中返回一个 JavaScript
DateTime
对象selectionInfo.start
。这是一个具有属性的对象。它不是预格式化的字符串。接下来发生的事情是,当您使用
document.getElementById('start').value = selectionInfo.start
该值将该值写入start
隐藏字段时,JS 必须将日期对象转换为一段文本。由于您没有为字符串化日期指定任何其他格式,因此它使用DateTime 类的默认toString 函数。这就是产生Mon Dec 30 2024 09:30:00 GMT+0200 (Ida-Euroopa standardaeg)
您所看到的字符串的原因。这与 fullCalendar 完全无关,完全取决于您编写代码的方式。传输日期的更好方法是使用标准 ISO8601 格式(例如
YYYY-MM-DDTHH:mm:ss.sssZ
),这种格式更易于解析,而且没有任何本地化负担。JavaScript Datetime 对象有一个函数可以以该格式输出日期。所以你需要做的就是写
反而。
那么你的 .NET 代码应该能够顺利解析日期。我现在无法测试,但你甚至可以写
作为方法签名,从而节省了手动调用的麻烦
DateTimeOffset.Parse()
。