2

I have a tree structure that contains css selectors, my goal is to take the value from the input element that matches the selector and put them into the tree. For example if there's a <input type="text/> with the id textbox and the value 67, I want to do this {selector: "#textbox"} -> {value: 67}.

I've got this working in all cases, except when the input type is date. In that case I want to convert the string to a date object. My code looks like this

val = sel.val();
let type = sel.attr("type");
if(type == "number")
{
    val = Number(val);
}
else
if(type == "date")
{
    val = Date(val);
}

//#1
ast["value"] = val;
//#2
delete ast.selector;

At point #1, when the input type is date, val is a Date object. At point #2, ast["value"]/ast.value is a Date object as well. However, printing the entire ast object shows ast["value"]/ast.value as a string. This string value propagates through the rest of the code and causes issues. This is a quote from the console showing this

Wed Jul 27 2016 10:14:19 GMT-0600 (MDT) eval.js:249 //ast["value"] at point #2
Object {selector: "this", value: "Wed Jul 27 2016 10:14:19 GMT-0600 (MDT)"} eval.js:250 //ast at point #2

If the date weren't getting implicitly converted, the output would be

Wed Jul 27 2016 10:14:19 GMT-0600 (MDT) eval.js:249 //ast["value"] at point #2
Object {selector: "this", value: Wed Jul 27 2016 10:14:19 GMT-0600 (MDT)} eval.js:250 //ast at point #2

I've tested this code in Chrome and Firefox, both yield the same result.

3
  • 2
    Are you sure it's a string? Might just be some console feature/"feature". Commented Jul 27, 2016 at 16:39
  • You can always cast it to a Date. Although Date.prototype.toString IIRC is implementation dependent, as long as you're constructing the Date from the platform's own string representation you should be fine. Commented Jul 27, 2016 at 16:41
  • Don't cast it to a Date when it's not a Date! That's just kicking the can down the road. Commented Jul 27, 2016 at 16:45

1 Answer 1

4

The Date() function takes a number or string and produces a string. Read the documentation:

JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax

You probably want to use new Date(val) instead of Date(val) as this actually produces a Date object.

Sign up to request clarification or add additional context in comments.

1 Comment

The Date function can take from zero to seven arguments. The behaviour changes depending on how many and what type (if only one provided, which might be a string, number or Date).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.