0

I have the following function in Javascript

function validateDates() {
Date fromDateObj = Date.parse(GetControlDate('CalDate'));
Date toDateObj = Date.parse(GetControlDate('ToDate'));
}

The two lines are causing a syntax error saying 'missing ';' before statement'. I can't figure out what's causing this, if I replace those lines with a simple alert statement it works fine, so I know the issue is in those lines. Also replacing those function calls GetControlDate('CalDate') with an actual date string doesn't fix the issue either. Can anyone tell me what the issue is?

1
  • 1
    Changing Date by var keyword should fix your problem Commented Sep 16, 2013 at 14:15

4 Answers 4

2

Javascript is weak type. all variable is "var" other than strong type in Java or C#. Hence use var instead of Date

function validateDates() {
    var fromDateObj = Date.parse(GetControlDate('CalDate'));
    var toDateObj = Date.parse(GetControlDate('ToDate'));
}
Sign up to request clarification or add additional context in comments.

Comments

2

JavaScript doesn't have "typed" variables. You don't need the Date before the variable name, you need var.

Also, Date.parse returns an int (the UNIX timestamp), not a Date object.

You want:

function validateDates() {
    var fromDateObj = new Date(GetControlDate('CalDate'));
    var toDateObj = new Date(GetControlDate('ToDate'));
}

Comments

1

Date is not a valid datatype for a variable use a weakly typed var.

Comments

1

Use var rather than Date to declare your variables:

function validateDates() {
    var fromDateObj = Date.parse(GetControlDate('CalDate'));
    var toDateObj = Date.parse(GetControlDate('ToDate'));
}

JS variables aren't fixed to a particular type of value and thus aren't declared with Date or int or whatever.

Comments

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.