Random Javascript Manipulation

I stumbled upon a seemingly respected source of a weird date manipulation that I don't understand. This is part of the documentation support documentation for the popular user interface:

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

Now line by line var startDate = start.value(); these lines return a Date object and store it in the startDate variable. Great, no problem here.

Then we create a new Date object with the same value and assign it to the same variable (slightly confusing, but I can live with it).

The third line is a real puzzle - we get the day of the month (via getDate ) and assign it as the day of the month (via setDate ) in the same variable.

Now the question is: is this bad code, perhaps the remnants from incomplete refactoring? Or does it really make sense, and he does some manipulation, like time removal (not like this)? If so, what is he doing?

UPD : sample code here http://demos.telerik.com/kendo-ui/datepicker/rangeselection

+7
javascript date
source share
2 answers

The source is available in several formats, and if we check them all:

html5 / javascript:

 startDate.setDate(startDate.getDate()); 

asp.net:

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

JSP:

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

PHP:

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

We can clearly see that the first (one of which you linked) stands out where they should be the same. This would make the problem a simple typo.

+3
source share

Allows you to go line by line:

 startDate = new Date(startDate); 

startDate the same date if startDate is 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 that all these 3 ways of representing Date are used, you will get a Date object.

 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 following line:

 startDate.setDate(startDate.getDate()); 

This makes no sense since it will return the same Date

 var startDate = new Date(); console.log(startDate); startDate.setDate(startDate.getDate()) console.log(startDate); 
+2
source share

All Articles