我在發佈帶有日期時間值的editform時遇到問題。我有一個用getTimeZonOffset設置localdatetime的腳本。第一次當我想從數據庫的utc時間顯示在本地時間它的工作原理。問題是當我想用datetimepicker編輯表單時。新值存儲在數據庫中,每次用戶點擊提交按鈕時,它都會再次添加本地時間。如何在發佈表單時將datetimepicker設置爲本地時間
我該如何避免這種情況?
$(function() {
// Determine timezone offset in milliseconds
var d = new Date();
var offsetms = d.getTimezoneOffset() * 60 * 1000;
$('.UTCTime input').each(function() {
try
{
var text = $(this).val();
var n = new Date(text);
n = new Date(n.valueOf() - offsetms);
var curr_date = n.getDate();
var curr_month = n.getMonth() + 1; //Months are zero based
var curr_year = n.getFullYear();
var curr_hour = n.getHours();
var curr_minutes = n.getMinutes();
$(this).val(curr_year + "/" + curr_month + "/" + curr_date + " " + curr_hour + ":" + curr_minutes);
}
catch (ex) {
console.warn("Error converting time", ex);
}
});
});
$(function() {
$(".datetimepicker input").datetimepicker({
dateFormat: "yy/mm/dd",
showTime: true,
});
});
<div class="span3 value UTCTime datetimepicker">
您是否已得到答案? –