What Does the Plus Sign Do in '+New Date'

What does the plus sign do in '+new Date'

That's the + unary operator. It's equivalent to:

function(){ return Number(new Date); }

See http://xkr.us/articles/javascript/unary-add and MDN.

What is the purpose of a plus symbol before a variable?

The + operator returns the numeric representation of the object. So in your particular case, it would appear to be predicating the if on whether or not d is a non-zero number.

Reference here. And, as pointed out in comments, here.

How does a prepending plus sign treat new Date?

+ converts the following expression into a number, if it can. If the following expression is an object, then that object's valueOf function is called, so as to returns the primitive value of the specified object, which can then be (attempted) to be coerced to a number.

How does a + sign affect new Date? How does the value Wed Nov 07 2018 21:50:30 GMT-0500 for example, convert into a numerical representation?

Date.prototype.valueOf returns the integer timestamp of the date object in question:

console.log(  new Date().valueOf());

Why the Javascript Date() constructor prefixes the output year with a plus sign if the year is above 9999

Is this correct?

The short answer is "yes".

Nothing to do with the Date constructor. Dates are just an offset in milliseconds from the ECMAScript epoch (1970-01-01T00:00:00Z), that's it.

What you are seeing is the result of calling Date.prototype.toISOString which creates a string for the date represented by the Date instance's time value as specified by ECMA-262 Date Time String Format.

For expanded years of more than 4 digits, the year is preceded by "+" or "-" as appropriate.

JavaScript plus sign in front of function expression

It forces the parser to treat the part following the + as an expression. This is usually used for functions that are invoked immediately, e.g.:

+function() { console.log("Foo!"); }();

Without the + there, if the parser is in a state where it's expecting a statement (which can be an expression or several non-expression statements), the word function looks like the beginning of a function declaration rather than a function expression and so the () following it (the ones at the end of the line above) would be a syntax error (as would the absense of a name, in that example). With the +, it makes it a function expression, which means the name is optional and which results in a reference to the function, which can be invoked, so the parentheses are valid.

+ is just one of the options. It can also be -, !, ~, or just about any other unary operator. Alternately, you can use parentheses (this is more common, but neither more nor less correct syntactically):

(function() { console.log("Foo!"); })();
// or
(function() { console.log("Foo!"); }());

d3.js prepended plus sign

It causes the object passed in, x, to be converted to a number. If a string is passed in it will attempt to convert it to a number before setting gravity.

For example, calling force.gravity('1'); will have the same result as calling force.gravity(1);.

See javascript: plus symbol before variable.

Date Range is starting with Plus

There are a couple of things going on that could account for the problem.

  • (This is probably the thing you're seeing) You're changing the state of the Date instance that you added to the array after adding it. That changes the instance that's in the array.
  • Since "2020-12-05" is a date-only string, it's parsed as UTC in specification-compliant browsers (see the rules at Date.parse), so the Date objects will represent midnight on that date UTC rather than midnight on that date in your local timezone. If you want them to reliably be midnight local time, add T00:00 to the string. If you want them to be parsed reliably as UTC, you should be able to use them as-is, but the spec moved around on this (ES2015 said they should be parsed as local time, not UTC, before being updated by ES2016) so to get UTC reliably, add T00:00Z to it. (My understanding is that the spec allows just adding a Z, but others read the spec differently, and both Safari's JavaScriptCore and Firefox's SpiderMonkey reject new Date("2020-12-05Z"), so add T00:00Z.) Or use new Date(Date.UTC(year, month - 1, day)) instead.
  • You're mixing up accessors here: loop.setDate(loop.getUTCDate() + 1). That's using the UTC accessor to get the date, but then the local time accessor to set it. You need to be consistent in order to add a day to the date.
  • You haven't said how you're using the resulting array of Date objects, but just note that the date they show may seem off if (for instance) you parse them as UTC but then display the result in local time or vice-versa, because of the time zone offset.
  • You have to and from reversed in your code. You're using them consistently, but it's quite hard to read. :-) When building an array in date order, it goes from the earlier date to the later date.

Here's a version updated to work entirely in UTC; the resulting Date objects represent midnight UTC on each of the dates:

function dateRangeArr(from, to) {
console.log(from);
console.log(to);
var dates = [];

var end = new Date(to + "T00:00Z");
var loop = new Date(from + "T00:00Z");

while (loop <= end) {
dates.push({ "total": 0, "log_date": new Date(+loop) });
loop.setUTCDate(loop.getUTCDate() + 1);
}
return dates;
}

console.log(dateRangeArr("2020-12-05","2020-12-12"));

Javascript date parse plus string

I went ahead and created a function myself since something else doesn't seem to exist.

function parseDate(date)
{
if(date == "now" || date == "")
{
return new Date();
}

if(date.charAt("0") == "+")
{
date.replace("+", "");
var cmp = date.split(" ");

if(cmp[1].indexOf("seconds") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 1000));
}
else if(cmp[1].indexOf("minutes") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 1000));
}
else if(cmp[1].indexOf("hours") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 1000));
}
else if(cmp[1].indexOf("days") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 1000));
}
else if(cmp[1].indexOf("weeks") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 7 * 1000));
}
else if(cmp[1].indexOf("months") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 30 * 1000));
}
else if(cmp[1].indexOf("years") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 365 * 1000));
}
}

if(date.charAt("0") == "-")
{
var cmp = date.split(" ");

if(cmp[1].indexOf("seconds") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 1000));
}
else if(cmp[1].indexOf("minutes") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 1000));
}
else if(cmp[1].indexOf("hours") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 1000));
}
else if(cmp[1].indexOf("days") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 1000));
}
else if(cmp[1].indexOf("weeks") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 7 * 1000));
}
else if(cmp[1].indexOf("months") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 30 * 1000));
}
else if(cmp[1].indexOf("years") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 365 * 1000));
}
}

return new Date(date);
}

This accepts the following inputs:

"now" | "": returns current date

Datestring: returns corresponding date (this already worked in new Date(dateString))

[+ | -][#] [timevar]: returns current time [+ | -] [#] of [timevar]
e.g. "+30 seconds"

Flow type, What does the `+` symbol mean in front a property?

The + symbol in front of the property means the property is read-only

Reference: https://flow.org/en/docs/types/interfaces/#toc-interface-property-variance-read-only-and-write-only



Related Topics



Leave a reply



Submit