日期时间 - 本地设置默认小时数

问题描述 投票:1回答:3

我正在使用以下输入:

<input id="creation-from-date" type="datetime-local"/>

我想知道是否有办法设置这样的默认小时数:dd / mm / yyyy 00:00

因为datepicker只允许选择日期,而不是时间,所以我在JS方面得到一个无效的日期,因为没有设置小时。

我正在使用Chrome v73.0.3683.75

非常感谢 !

javascript datetime datetimepicker
3个回答
1
投票

我会用<input type='date' /><input type='time' />代替:

//<![CDATA[
/* external.js */
var doc, bod, I, DateTime; // for use on other loads
addEventListener('load', function(){
doc = document; bod = doc.body;
I = function(id){
  return doc.getElementById(id);
}
DateTime = function(dateElement, timeElement, dateInstance){
  var t = this;
  this.dateElement = dateElement; this.timeElement = timeElement;
  this.date = dateInstance instanceof Date ? dateInstance : new Date;

  this.dateValue = function(dateInstance){
    if(dateInstance instanceof Date)this.date = dateInstance;
    var dt = this.date;
    return dt.getFullYear()+'-'+(dt.getMonth()+1).toString().replace(/^(\d)$/, '0$1')+'-'+dt.getDate().toString().replace(/^(\d)$/, '0$1');
  }
  this.showDate = function(dateInstance){
    this.dateElement.value = this.dateValue(dateInstance);
    return this;
  }
  this.timeValue = function(dateInstance){
    if(dateInstance instanceof Date)this.date = dateInstance;
    var dt = this.date;
    return dt.getHours().toString().replace(/^(\d)$/, '0$1')+':'+dt.getMinutes().toString().replace(/^(\d)$/, '0$1');
  }
  this.showTime = function(dateInstance){
    this.timeElement.value = this.timeValue(dateInstance);
    return this;
  }
  this.showDate().showTime();
  this.dateChange = function(changeFunc, noTimeFunc){
    this.dateElement.oninput = function(){
      var v = this.value, s = t.timeElement.value;
      if(v === '')v = this.value = t.dateValue(noTimeFunc(t));
      if(s === '')s = t.timeValue(this.date);
      t.date = new Date(v+' '+s); changeFunc(t.date, t);
    }
    return this;
  }
  this.timeChange = function(changeFunc, noTimeFunc){
    this.timeElement.oninput = function(){
      var v = this.value, s = t.dateElement.value;
      if(v === '')v = this.value = t.timeValue(noTimeFunc(t));
      if(s === '')s = t.dateValue(this.date);
      t.date = new Date(s+' '+v); changeFunc(t.date, t);
    }
    return this;
  }
}
var dateElement = I('date'), timeElement = I('time');
function threeHoursLater(){
  return new Date(Date.now()+10800000);
}
var dt = new DateTime(dateElement, timeElement, threeHoursLater()); //  3 hours from now - initial date time set
function consoleIt(dateInstance){
  console.log('display of dt.date --> '+dateInstance.toString());
  console.log('dt.date for server --> '+dateInstance.getTime());
}
consoleIt(dt.date);
dt.dateChange(function(r){
  consoleIt(r);
}, threeHoursLater).timeChange(function(a){
  consoleIt(a);
}, threeHoursLater);
}); // end load
//]]>
<input id='date' type='date' />
<input id='time' type='time' />

关闭这些输入,看看会发生什么!哦,请确保您在服务器上验证这些日期。客户可以更改。

我已经更新了上面的代码以包含一个DateTime构造函数。争论应该是明确的。

PS

我注意到Firefox 67.0(64位)中存在更改事件的问题,在没有首先获得焦点的元素上,因此.onchange被更改为.oninput,这似乎全面工作。


0
投票

您可以尝试使用Javascripts日期:

$("creation-from-date").val(new Date().toLocalString());
// toLocalString() will return the local time while toISOString() will return the UTC time.

这里有一个很好的jQuery extension,对于初始化datetimedatetime-local输入非常有用:

// To initialize, simply call the method:
$('input[type="datetime-local"]').setNow();  

希望这可以帮助。


0
投票

尝试为其添加默认日期

document.getElementById("creation-from-date").value = setDefautValue();
    function setDefautValue() {
        var date = new Date(); // today
        date.setUTCHours(0, 0, 0, 0); //set default time 00:00 AM
        const dStr = date.toISOString()
        // remove seconds and milliseconds
        return dStr.substring(0, dStr.indexOf(':', dStr.indexOf(':')+1))
    }
© www.soinside.com 2019 - 2024. All rights reserved.