我自己写了这个特殊代码来获取当前时间:
$(function() {
function getCurrentTime() {
var CurrentTime = "";
try {
var CurrentDate = new Date();
var CurrentHours = CurrentDate.getHours();
var CurrentMinutes = CurrentDate.getMinutes();
var CurrentSeconds = CurrentDate.getSeconds();
if (CurrentMinutes < 10) { CurrentMinutes = "0" + CurrentMinutes; }
if (CurrentSeconds < 10) { CurrentSeconds = "0" + CurrentSeconds; }
if (CurrentHours < 10) { CurrentHours = "0" + CurrentHours; }
CurrentTime = "" + CurrentHours + ":" + CurrentMinutes + ":" + CurrentSeconds + "";
}
catch (ex) {
}
return CurrentTime;
}
并且我设法在与datepicker相同的输入字段中显示的唯一方法是:
$(".class").datepicker({
autoclose: true,
format: "dd.mm.yyyy" + " " + getCurrentTime(),
}
这一切都很好,但我无法编辑时间的内容,因为当datepicker关闭时,它总是返回当前时间。请帮助我找到解决方法,不使用其他插件。
答案 0 :(得分:0)
不确定我明白你的目标是什么。用户应该手动输入时间吗?
getCurrentTime函数中的这一变化将检查在datepicker当前值中是否存在“时间”部分 - 如果有这样的时间部分,它将使用该文本而不是重新计算它。
因为我没有机会尝试它可能是错误的,但这可能是一个可能的解决方案。不是最好的,因为使用“格式”属性不是实现这一目标的最佳方法,但它应该有效。
$(function() {
function getCurrentTime() {
var CurrentTime = "";
try {
var currTime = undefined;
var currVal = $('#my-datepicker').val();
if (currVal.length > 11) {
currTime = currVal.substring(11, 8);
}
if (typeof currTime !== 'undefined' && currTime.length == 8) {
CurrentTime = currTime;
} else {
var CurrentDate = new Date();
var CurrentHours = CurrentDate.getHours();
var CurrentMinutes = CurrentDate.getMinutes();
var CurrentSeconds = CurrentDate.getSeconds();
if (CurrentMinutes < 10) { CurrentMinutes = "0" + CurrentMinutes; }
if (CurrentSeconds < 10) { CurrentSeconds = "0" + CurrentSeconds; }
if (CurrentHours < 10) { CurrentHours = "0" + CurrentHours; }
CurrentTime = "" + CurrentHours + ":" + CurrentMinutes + ":" + CurrentSeconds + "";
}
}
catch (ex) {
}
return CurrentTime;
}
答案 1 :(得分:0)
如果我理解正确的问题,HTML5输入类型“日期”是最简单的解决方案之一,没有任何插件。
<input type="date">
用于应包含日期的输入字段。
示例:
<input type="date" name="bday" min="2000-01-02">
有一个类似的解决方案 Input type DateTime - Value format?