简体   繁体   English

使用javascript在HTML输入(日期)类型中操作日期

[英]Manipulating the date in an HTML input(date) type using javascript

I have an HTML date input type and I'd like to manipulate it programatically in javascript. 我有一个HTML日期输入类型,我想在javascript中以编程方式操作它。 I'm just not getting it. 我只是没有得到它。

I see the most commonly accepted way of manipulating the date itself is like: 我看到最常用的操作日期的方法就像:

var c = new Date();
c.setDate(c.getDate() + 1);

I can get the date from the input: 我可以从输入中获取日期:

c = document.getElementById("date1").valueAsDate

I can set the date of the input: 我可以设置输入的日期:

document.getElementById("date2").valueAsDate = c

I can manipulate the date in c : 我可以在c操纵日期:

c.setDate(c.getDate()+1)

Using c as an interim value, I can manipulate the value of one date object with respect to the other: 使用c作为临时值,我可以相对于另一个操纵一个日期对象的值:

c = document.getElementById("date1").valueAsDate
c.setDate(c.getDate()+1)
document.getElementById("date2").valueAsDate = c

but no amount of manipulating the elements themselves am I able to transfer the date from one object to the other without using an interim variable. 但是没有操纵元素本身的数量我能够在不使用临时变量的情况下将日期从一个对象传输到另一个对象。

For example, although: 例如,尽管:

c = document.getElementById("date1").valueAsDate

and

document.getElementById("date2").valueAsDate = c

are both valid, they are not directly manipulatable: 两者都有效,它们不能直接操作:

document.getElementById("date1").valueAsDate 
        = document.getElementById("date2").valueAsDate  

The above is INVALID 以上是无效的

I seem to get close at times but I always need that interim variable. 我似乎有时接近但我总是需要那个临时变量。

I suppose I have 2 questions: 我想我有两个问题:

  1. What am I not understanding with these values that make them non transferable (between themselves)? 我不理解这些使它们不可转移的价值(它们之间)?

  2. What is the "cleanest" code to take the value from the document element, manipulate it by adding one day and sending it back 从文档元素中获取值的“最干净”代码是什么,通过添加一天并将其发回来对其进行操作

Copying the value using valueAsDate is within the HTML specification ( latest editor's draft ) and is possible in Chrome. 使用valueAsDate复制值在HTML规范最新编辑器的草稿 )中,并且可以在Chrome中使用。 However, Date inputs are not well supported yet so you should include feature testing to avoid errors and provide a fallback where features you want to use aren't supported. 但是,日期输入尚未得到很好的支持,因此您应该包括功能测试以避免错误,并提供不支持您要使用的功能的后备。

"Clean" code is not necessarily the shortest or least code. “干净”代码不一定是最短或最少的代码。 I would aim for robustness, readability and maintainability over "clean". 我的目标是“干净”的稳健性,可读性和可维护性。

To that end, there is no need to to use valueAsDate to copy the value from one input to another, just assign the value. 为此,无需使用valueAsDate将值从一个输入复制到另一个输入,只需赋值即可。 That works everywhere. 这无处不在。

 // Copy using value from one input to another // Supported everywhere function copyValue() { document.getElementById('i1').value = document.getElementById('i0').value; } // Copy using valueAsDate from one input to another // Throws errors where valueAsDate is not supported function copyDate(){ document.getElementById('i1').valueAsDate = document.getElementById('i0').valueAsDate; } // Copy using valueAsDate and add a day // Include simple feature test, no fallback function copyAddDate(){ var d = document.getElementById('i0').valueAsDate; // If valueAsDate not supported, return if (d === null) { // Provide fallback console.log('valueAsDate not supported'); return; } d.setDate(d.getDate() + 1); document.getElementById('i1').valueAsDate = d; } 
 Date: <input type="date" id="i0" value="2017-08-20"><br> Date: <input type="date" id="i1" readonly> <button onclick="copyValue()">Copy value</button> <button onclick="copyDate()">Copy date</button> <button onclick="copyAddDate()">Copy and add 1 day</button> 

However, input type date is not well supported (eg Safari, Firefox 49), so while this works in the latest Chrome browser, it doesn't in others. 但是,输入类型日期不受支持(例如Safari,Firefox 49),因此虽然这在最新的Chrome浏览器中有效,但在其他浏览器中则不然。

So to answer your questions: 那么回答你的问题:

What am I not understanding with these values that make them non transferable (between themselves)? 我不理解这些使它们不可转移的价值(它们之间)?

Nothing, they should be in a compliant implementation. 没什么,他们应该在一个合规的实现。 However, there aren't many of those. 但是,这些并不多。

What is the "cleanest" code to take the value from the document element, manipulate it by adding one day and sending it back 从文档元素中获取值的“最干净”代码是什么,通过添加一天并将其发回来对其进行操作

I think you already know that, but you should include some feature testing: 我想你已经知道了,但你应该包括一些功能测试:

var c = document.getElementById("date1").valueAsDate;
// Check that a Date was returned
if (Object.prototype.toString.call(c) == '[object Date]') {
  c.setDate(c.getDate()+1);
  document.getElementById("date2").valueAsDate = c;
} else {
  console.log('Didn\'t get a Date');
}
  1. I don't know the actual implementation, but I'm pretty sure valueAsDate returns a new Date that is a copy of the element's date and isn't a direct reference. 我不知道实际的实现,但我很确定valueAsDate返回一个新的Date ,它是元素日期的副本,不是直接引用。 valueAsDate is most likely just a getter/setter and not an actual property, meaning things like setDate won't mutate the underlying value as it's operating on a completely new, independent. valueAsDate很可能只是一个getter / setter而不是一个实际的属性,这意味着像setDate这样的东西不会改变底层值,因为它在一个全新的独立操作上运行。 So you need to retrieve the date, mutate it with setDate , then reassign the element's date to this newly mutated date. 因此,您需要检索日期,使用setDate对其进行变更,然后将元素的日期重新分配给此新变异的日期。

  2. I would do exactly that. 我会这样做的。 Write some kind of nice function like 写一些很好的功能就好

     function addDaysToDateElement(element, daysToAdd) { var date = element.valueAsDate; date.setDate(date.getDate() + 1) element.valueAsDate = date; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM