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:
var startDate = start.value(); // returns Date object
startDate = new Date(startDate);
startDate.setDate(startDate.getDate());
Now line by line var startDate = start.value();
this lines returns Date object and saves it in startDate
variable. 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).
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.
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
Javascript doesn't actually Support Dates # Javascript only supports date times. All Javascript dates are Unix timestamps underneath. That means if we try to create a date, we actually are creating a date time. All Javascript dates with no time specified default to midnight on that given day.
You can pass parameters to the Date constructor to create a date of your choice. The given parameter can take different forms. You can pass a date string of an accepted format when creating a new Date object. const date = new Date (“2020-12-31”);
The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC). What Z means in date? zero. What is a timestamp example? TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
The source is available in multiple formats, and if we inspect 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 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
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.
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
var startDate = new Date();
console.log(startDate);
startDate.setDate(startDate.getDate())
console.log(startDate);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With