Javascript syntax error in Date object - javascript

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?

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'));
}

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'));
}

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

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.

Related

When try to pass date the slashes occur division

I have a variable var that contains a date "29/5/2017" in my page. Now i am trying to pass this variable to a function that is stored in a separate js file. My issue is when i debug the js i see a number 0,0028... The js believes that this is a number a makes a division. how can i prevent this and pass just the date?
the var in my aspx file is :
var mydates = '29/5/2017';
the function call in my aspx file
calldate(mydates);
the function in the js file
function calldate(mydates) {
alert(mydates);
}
you need to use double quotations to single quotations mark during init variable
var mydates = "29/5/2017".toSting();
and user .toString() function for canvart to string.
Thanks
Instead of passing date as string try to pass variable as Date datatype.
there might be chances that the variable getting changed before calling the function.
<pre>
var mydates = Date("29/5/2017");
</pre>

How to parse a Javascript datetime to C# DateTime parameter

According to my previous question here: How to send a JSON object using GET method, I could retrieve my values because the object is no longer null. My object contains one DateTime property called CreatedOn. I am sending the value from javascript as new Date(). Before to answer that I can get the DateTime.Now() from code-behind, there is a purpose to send the date from HTML.
Then, in debug mode when I arrive to the controller method, my CreatedOn property is always DateTime.MinValue = 01/01/0001 12:00:00 AM
I changed my javascript value to this format "yyyyMMddT000000" because I thought that this would be parsed automatically but I didn't have any success.
How can I do to send the value that can be parsed by the web api2 controller automatically?
<script>
$("#btnTest").on("click", function () {
var searchCriteria = {};
searchCriteria.ID = 0;
searchCriteria.Name = "";
//1. First tried option
//searchCriteria.CreatedOn = new Date();
//2. Second tried option. Test
searchCriteria.CreatedOn = "20170324T000000";
var url = "http://localhost:8080/api/products"
$.getJSON(url, searchCriteria).done(processResponse);
});
function processResponse(response){
}
</script>
I got it. Hope this can help others.
searchCriteria.CreatedOn = new Date().toISOString();
This will be parsed automatically.
Cheers.

javascript, turn string into code?

First question, might be dumb, be easy on me.
So I have an object creator followed by a set of variables, a-z, which create 26 objects with names a-z. I have a piece of code which generates a random letter from a-z and i would like to display a property of the randomly picked object, but the letter is generated as a string, and "a".type comes out as undefined (understandably). so i need to take that string, and remove the quotes from it basically so i can use it. i found somewhere on here someone said ("a").charAt(0) would work but it doesn't in my case. heres a very simplified version of the code
function Object(type) {
this.type = type;
};
var a = new Object("annoying");
var random = "a";
console.log((random).type);
I just want it to log "annoying" but i believe that chance.character is making it log "a".type instead of a.type. So i need a way to turn the string ito a usable piece of code so i can refer to the variable.
Unless I'm misunderstanding the question, you should be able to store your objects by their corresponding letter (as a key) in a containing object like so:
var letterObjects = {};
function letterObject(type) {
this.type = type;
};
letterObjects["a"] = new letterObject("annoying");
var random = (chance.character);
console.log(letterObjects[random].type); //annoying

TypeError: Date is not a constructor

So, I've been making forms for my company for some time now with pretty easy Javascript that has worked for me in the past. However all of a sudden it's kicking out the error: TypeError: Date is not a constructor
The Code:
var Date = this.getField("Text1");
Date.value = util.printd("mm/dd/yyyy",new Date());
It works on all my old forms, but now it won't work on new ones... and I've tried making a new button on an old form - just copying and pasting the code, and then it'll break all the other buttons and spit out the same error.
Running: Windows 7 64-bit with Acrobat XI 11.0.10
The variable Date is hiding the global function Date and causing this error. Because of how scoping works in JS, the inner-most use of a name is the one that matters.
In this case, you declare var Date which becomes the only Date the function knows about. When you assign it a field or text (Date = this.getField...), you hide the global class.
You can rename your variable (I would suggest date, as capital names are typically reserved for types) or explicitly reference new window.Date when you go to construct a new date.
This worked for me:
var d = new window.Date();
Might be this answer will be helpful in future. I was using below code
var dateTime=new date();
But right code is
var dateTime=new Date();
You can't define a variable called "Date" because there's a built-in object in JS called that (you're using it in your code, actually). Change the name to something else.
var Date= somthing; <-- wrong declare, you should not use build -in object name
I was having this problem and I solved it! don't use "Date" as variable because this causes conflict with Global function Date();
Exemple: Wrong !
var Date = new Date();
document.getElementById('dateCopy').innerHTML = Date.getFullYear();
Right:
var DateTime = new Date();
document.getElementById('dateCopy').innerHTML = DateTime.getFullYear();
In your case:
var DateTime = this.getField("Text1");
DateTime.value = util.printd("mm/dd/yyyy",new Date());

Javascript Computed Values With Arrays

Jquery Each Json Values Issue
This question is similar to above, but not the same before it gets marked duplicated.
After realasing how to use computed values i came across another issue.
In my javascript i have the following code:
var incidentWizard = ['page1.html','page2.html','page3.html'];
var magicWizard = ['page1.html','page2.html','page3.html'];
var loadedURL = 'page1.html';
The input to this function would be (true,'incident')
function(next,wizardname)
{
var WizSize = incidentWizard.length;
wizardName = [wizardName] + 'Wizard';
var wizardPOS = jQuery.inArray(loadedURL,incidentWizard);
And now i want to use the wizardname parameter to decide what array i am going to use...
Loader(incidentWizard[wizardPOS],true);
Ive also tried
Loader([incidentWizard][wizardPOS],true);
and
Loader([incidentWizard][wizardPOS],true);
Also the loader function just required the string value in the array at wizardPOS sorry for confusion
But when trying this i always end up with the outcome...
/incidentWizard
I know this is something to do with using computed values but i've tried reading about them and cant seem to solve this issue.
Basicly i want to use the computed value of wizardName to access an an array of that name.
Please help supports, looking forward to seeing many ways to do this!
On this line:
wizardName = [wizardName] + 'Wizard';
You are attempting to concatenate the string 'Wizard' to an Array with one string element "incident". I'm assuming you just want regular string concatenation:
wizardName = wizardName + 'Wizard';
However, now you only have a string, not an array instance. To fix that, change the way you define your *Wizard arrays to something like:
var wizardyThings = {
incidentWizard : ['page1.html','page2.html','page3.html'],
magicWizard: ['page1.html','page2.html','page3.html']
};
Then your function (which is missing a name as it stands), becomes:
function someMethod(next, wizardname) {
wizardName = wizardName + 'Wizard';
var wizSize = wizardyThings[wizardName].length;
var wizardPOS = jQuery.inArray(loadedURL, wizardyThings[wizardName]);
...
}
You can only access properties of objects that way. For global values, window[ name ] will work. For simple local variables it's just not possible at all. That is, if inside a function you've got
var something;
then there's no way to get at that variable if all you have is the string "something".
I would just put each array as a prop on an object:
var obj {
incidentWizard: ['page1.html','page2.html','page3.html'],
magicWizard: ['page1.html','page2.html','page3.html']
};
Then you can just do obj['incidentWizard'] or obj.incidentWizard this will return:
['page1.html','page2.html','page3.html']

Categories

Resources