[英]How to get date from innerHTML / textContent javascript
Here is the JavaScript code that I am trying to use to get a date from inside a element populated with several 这是我试图用来从填充了几个元素的元素内部获取日期的JavaScript代码
elements. 元素。 The 9th element contains a date in YYYY-MM-DD
format. 第9个元素包含YYYY-MM-DD
格式的日期。 When I use this code, it doesn't seem to be converting the elements into a JavaScript date -- instead I get [native code] when I try to dump the date variable. 当我使用此代码时,似乎并没有将元素转换为JavaScript日期-而是在我尝试转储date变量时得到了[本机代码]。 Any ideas? 有任何想法吗?
Partial code: 部分代码:
if (optvalue == 2) {
for (i = 0; i < trans_array.length; i++) {
var cn = trans_array[i].childNodes;
var dts = cn[9];
try {
var transdt = new Date(dts.innerHTML);
if (transdt < prior_month) {
trans_array[i].classList.add("hide");
alert("node hiddent!");
}
alert(transdt.getDay + " / " + transdt.getMonth + " / " + transdt.getFullYear);
} catch(err) {
alert("Error: Inner HTML is not a date:" + dts.innerHTML)
}
}
alert("Value is 2");
}
<div class="transaction_item prior_day" id="trans-88">
<!----------- Sub Row 1 ------------------->
<p class="tiday">Tue</p>
<p class="tipayee">Coutry Boy Gas</p>
<p class="ticredit"> </p>
<p class="tidebit">7.02</p>
<p class="tibalance">1.24</p>
<p class="solid_box" id="check-88" onclick="toggle_checkmark(88)"> </p>
<p onclick="edit(1, 88)">Edit</p>
<!-------------- Sub Row 2 --------------------->
<p class="tidate">2018-04-17</p>
<p class="ticat">Miscellaneous Expense</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p class="timemo">Gas for Car </p>
</div>
If I understood correct, you want to get a string date and convert to a date()
, right? 如果我理解正确,那么您想获取一个字符串date并转换为date()
,对吗? If yes, why not get the value directly from the element with class .tidate
then create a new date()
? 如果是,为什么不直接从类.tidate
的元素中获取值,然后创建一个new date()
?
This will return a complete date with day, month, year and time, to get just what you need, you can use the get methods from the date()
, like: 这将返回包含日,月,年和时间的完整日期,以获取所需的信息,您可以使用date()
的get方法,例如:
getDate()
: Returns the day of the month (1-31) getDate()
:返回月份中的某天(1-31)
getFullYear()
: Returns the year (4-digit years) getFullYear()
:返回年份(4位数字的年份)
getMonth()
: Returns the month (0-11) getMonth()
:返回月份(0-11)
Basic Example: 基本示例:
var date_string = document.querySelector('.tidate').textContent; var nDate = new Date(date_string); console.log("Complete Date: " + nDate); console.log("Day:" + nDate.getDate()); console.log("Month:" + nDate.getMonth()); //ADD +1 to get "real life" month console.log("Year:" + nDate.getFullYear());
<!----------- Sub Row 1 -------------------> <p class="tiday">Tue</p> <p class="tipayee">Coutry Boy Gas</p> <p class="ticredit"> </p> <p class="tidebit">7.02</p> <p class="tibalance">1.24</p> <p class="solid_box" id="check-88" onclick="toggle_checkmark(88)"> </p> <p onclick="edit(1, 88)">Edit</p> <!-------------- Sub Row 2 ---------------------> <p class="tidate">2018-04-17</p>
If I understood wrong, please, clarify to me what exactly is your need or post a code that we can reproduce the issue in a snippet. 如果我理解有误,请向我说明您的确切需求,或发布一个代码,以使我们可以在代码段中重现该问题。
As per my understanding I have made code modification and created a fiddle. 根据我的理解,我已经进行了代码修改并创建了一个小提琴。
Here is the working Fiddle: https://jsfiddle.net/kh9pandv/ 这是工作中的小提琴: https : //jsfiddle.net/kh9pandv/
Modifications: 修改:
var dts = cn[9];
to 至
var dts = trans_array.querySelector('.tidate');
In alert, you were not calling functions, you just named the function. 在警报中,您没有调用函数,只是给函数命名。
alert(transdt.getDay() + " / " + transdt.getMonth() + " / " + transdt.getFullYear());
I believe that the problem is that you're accessing the property "getDay" instead of calling the function "getDay()" 我认为问题在于您正在访问属性“ getDay”,而不是调用函数“ getDay()”
Try changing your line 尝试换行
alert(transdt.getDay + " / " + transdt.getMonth + " / " + transdt.getFullYear);
to 至
alert(transdt.getDay() + " / " + transdt.getMonth() + " / " + transdt.getFullYear());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.