[英]How to correct the time on Fullcalendar using Ajax And Flask?
當我在日歷上進行約會時,開始時間和結束時間正確顯示,但是重新加載頁面后,我在日歷上和數據庫內部獲得了不同的日期時間。
因此,例如,如果我從09:00 AM到10:30 AM進行了約會,在數據庫中我得到了:
開始時間 :2017-04-30 15:00:00
結束時間 :2017-04-30 16:30:00
我的view.py :
appointment = Appointment()
appointment.start_time = parse_appoitment(start)
appointment.end_time = parse_appoitment(end)
appointment.note = data
appointment.user_id = abonent.id
appointment.client_id = client.id
db.session.add(appointment)
db.session.commit()
還有日期解析器 :
def parse_appoitment(time):
start_datime = str(time)[0:25]
date = parse(start_datime)
return date.strftime('%Y-%m-%d %H:%M:%S')
這是javascript
代碼:
{% if client.is_subscriber(master.id) %}
$(document).ready(function() {
var initialLocaleCode = 'ru';
var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listMonth'
},
defaultDate: moment(),
// month basicWeek basicDay agendaWeek agendaDay listYear listMonth listWeek listDay
defaultView: 'agendaWeek',
height: 650,
locale: initialLocaleCode,
local: 'GMT+06:00',
navLinks: true, // can click day/week names to navigate views
selectable: true,
selectHelper: true,
select: function(start, end) {
if(start.isBefore(moment())) {
$('#calendar').fullCalendar('unselect');
return false;
} else {
$(this).css({"cursor":"pointer"});
}
var title = prompt('Event Title:');
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
// console.log(eventData);
$.ajax({
type: 'POST',
url: '{{url_for("client.get_appo", abonent=master.slug)}}',
// contentType: 'application/json;charset=UTF-8',
data: {
'title': title,
'start': eventData['start']['_d'],
'end': eventData['end']['_d']
}
})
.done(function(data){
if(data.error){
// console.log(data.data);
console.log(eventData['start']['_d']);
}
else {
// console.log(data.data);
console.log(eventData['start']['_d']);
}
});
}
},
editable: true,
eventLimit: true, // allow "more" link when too many events
events: [
{% for appo in master.appointments %}
{% if appo.is_not_accepted %}
{% endif %}
{
"title": "{{appo.note}}",
"start": "{{appo.start_time}}",
"end": "{{appo.end_time}}"
},
{% endfor %}
]
});
// build the locale selector's options
$.each($.fullCalendar.locales, function(localeCode) {
$('#locale-selector').append(
$('<option/>')
.attr('value', localeCode)
.prop('selected', localeCode == initialLocaleCode)
.text(localeCode)
);
});
// when the selected option changes, dynamically change the calendar option
$('#locale-selector').on('change', function() {
if (this.value) {
$('#calendar').fullCalendar('option', 'locale', this.value);
$(this).css({'background-color':'red'});
}
});
});
{% endif %}
我不確定此local: 'GMT+06:00',
添加了它,或者我每次都得到的不是一樣的東西。
編輯:
我忘記輸入提交約會后和解析之前的格式:
Sun Apr 23 2017 15:30:00 GMT+0600 (+06)
Sun Apr 23 2017 17:00:00 GMT+0600 (+06)
Sun Apr 23 2017 15:30:00 GMT+0600 (+06)
至Sun Apr 23 2017 17:00:00 GMT+0600 (+06)
在這里,我從09:30到11:00做了一個,但是我得到了上述結果。
您必須在設置中將時區設置為本地,如下所示:
timezone: 'local'
現在它可以工作了:)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.