简体   繁体   English

Javascript奇怪的日期操作

[英]Javascript strange date manipulation

I came across in a seemingly reputable source a strange date manipulation that I don't understand. 我在一个看似有信誉的来源中遇到了一个奇怪的日期操作,我不明白。 This is part of the samples in supporting documentation for a popular UI framework: 这是流行UI框架的支持文档中的示例的一部分:

var startDate = start.value();  // returns Date object
startDate = new Date(startDate);
startDate.setDate(startDate.getDate());

Now line by line var startDate = start.value(); 现在逐行var startDate = start.value(); this lines returns Date object and saves it in startDate variable. 这一行返回Date对象并将其保存在startDate变量中。 Fine here, no problem. 好在这里,没问题。

Then we create new Date object with the same value and assign it back to the same variable (mildly confusing, but I can live with it). 然后我们创建具有相同值的新Date对象并将其分配回同一个变量(稍微有些混乱,但我可以忍受它)。

Third line is a real puzzle - we get day of the month (via getDate ) and assign it as a day of the month (via setDate ) in the same variable. 第三行是一个真正的难题 - 我们得到一个月中的某天(通过getDate )并将其作为一个月中的一天(通过setDate )分配到同一个变量中。

Now the question: is this a bad code, possibly leftovers from unfinished refactoring? 现在的问题是:这是一个糟糕的代码,可能是未完成的重构遗留下来的吗? Or is this really making sense and it does some manipulation like removing time (does not look like it)? 或者这是否真的有意义,它做了一些操作,如删除时间(看起来不像)? If it is, what does it do? 如果是,它会做什么?

UPD : code sample is here http://demos.telerik.com/kendo-ui/datepicker/rangeselection UPD :代码示例在http://demos.telerik.com/kendo-ui/datepicker/rangeselection

The source is available in multiple formats, and if we inspect them all: 源有多种格式,如果我们检查它们:

html5/javascript: HTML5 / JavaScript的:

startDate.setDate(startDate.getDate());

asp.net: asp.net:

startDate.setDate(startDate.getDate() + 1);

jsp: JSP:

startDate.setDate(startDate.getDate() + 1);

php: PHP:

startDate.setDate(startDate.getDate() + 1);

We can clearly see the first (one you linked to) stands out where they should be the same. 我们可以清楚地看到第一个(你链接到的)突出的地方应该是相同的。 This would lead one to believe the issue is a simple typo. 这将使人们相信这个问题是一个简单的错字。

Lets go line by line: 让我们一行一行:

startDate = new Date(startDate);

Will return the same date if startDate is a Date 如果startDateDate则返回相同的Date

 var someDate = new Date(5555555113); console.log(someDate); startDate = new Date(someDate); console.log(startDate); 

But if start.value() returns either miliseconds, or a string, passing it to new Date will ensure whatever of these 3 ways to represent a Date is used, you'll get the Date object. 但是,如果start.value()返回start.value()或字符串,将其传递给new Date将确保使用这3种表示Date方式中的任何一种,您将获得Date对象。

 var someDate = 5555555113; var startDate = new Date(someDate); console.log(startDate); someDate = "1970-03-06T07:12:35.113Z"; startDate = new Date(someDate); console.log(startDate); 

Now the next line: 现在下一行:

startDate.setDate(startDate.getDate());

This doesn't make any sense, as it'll return the same Date 这没有任何意义,因为它将返回相同的Date

 var startDate = new Date(); console.log(startDate); startDate.setDate(startDate.getDate()) console.log(startDate); 

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

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