[英]How can I write a editable time code in Javascript in the same input field as Datepicker, but without using Datetimepicker
I myself wrote this particular code to get the current time: 我自己编写了以下特定代码以获取当前时间:
$(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;
}
and the only way I managed to show in the same input field as datepicker was this: 我设法在与datepicker相同的输入字段中显示的唯一方法是:
$(".class").datepicker({
autoclose: true,
format: "dd.mm.yyyy" + " " + getCurrentTime(),
}
This is all fine and well, but I cannot edit the content of the time, as when the datepicker closes it always returns the current time. 一切都很好,但是我不能编辑时间的内容,因为当日期选择器关闭时,它总是返回当前时间。 Please help my find a way around this, WITHOUT using another plug in.
请帮助我找到解决此问题的方法,而无需使用其他插件。
Not sure I understood what your goal is. 不确定我了解您的目标是什么。 Should the user input manually the time?
用户应该手动输入时间吗?
This change in the getCurrentTime function will check if in the datepicker current value there is a "time" part - if there is such time part, it will use that text instead of recomputing it. getCurrentTime函数中的此更改将检查datepicker当前值中是否有“时间”部分-如果有这样的时间部分,它将使用该文本而不是重新计算它。
Could be buggy since I've had no chance to try it, but this could be a possible solution. 由于我没有机会尝试,因此可能会出现故障,但这可能是一种解决方案。 Not the best since using that "format" property isn't the best way to achieve this, but it should work.
这不是最好的方法,因为使用该“格式”属性不是实现此目的的最佳方法,但它应该可以工作。
$(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;
}
IF i understand the problem correctly, HTML5 input type "date" is one of easiest solution which work without any plugin. 如果我正确理解问题,则HTML5输入类型“日期”是最简单的解决方案之一,无需任何插件即可工作。 The
<input type="date">
is used for input fields that should contain a date. <input type="date">
用于应包含日期的输入字段。
example : <input type="date" name="bday" min="2000-01-02">
示例:
<input type="date" name="bday" min="2000-01-02">
One similar post with solution is there Input type DateTime - Value format? 一种类似的解决方案是输入类型DateTime-值格式?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.