Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 880
trailmax Avatar asked Aug 12 '16 14:08

trailmax


People also ask

What's wrong with JavaScript date?

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.

How do you manipulate a date object?

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”);

What is 000z in timestamp?

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.


2 Answers

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.

like image 50
aw04 Avatar answered Oct 17 '22 21:10

aw04


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);
like image 34
Marco Scabbiolo Avatar answered Oct 17 '22 23:10

Marco Scabbiolo