Type error in js [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I typed the following code
var curdte=new Date();
var curdteyr=curdte.getYear();
var curdtemh=curdte.getMonth();
var curdtedy=curdte.getDay();
var dtecurr=(curdtedy "-" curdtemh "-" curdteyr);
var fyear1=fromdat1.substring(6,10,10);
var fmonth1=fromdat1.substring(3,5,10);
var fday1=fromdat1.substring(0,2,10);
var fmdiff=(fday1 "-" fmonth1 "-" fyear1);
var frmdate1=(fyear1 "-" fmonth1 "-" fday1);
var tyear1=todat1.substring(6,10,10);
var tmonth1=todat1.substring(3,5,10);
var tday1=todat1.substring(0,2,10);
var todiff=(tday1 "-" tmonth1 "-" tyear1);
var todate3=(tyear1 "-" tmonth1 "-" tday1);
var oneday=24*60*60*1000;
var frmdiff=new Date(Math.ceil(dtecurr.getTime()-fmdiff.getTime())/oneday);
Then I get the error :
TypeError: dtecurr.getTime is not a function

That is because dtecurr is not a Date object, it should be a formatted string but is missing the + for concatenation. I've passed dtecurr to Date.parse, which should create a valid Date object to call getTime on:
var frmdiff=new Date(Math.ceil(Date.parse(dtecurr).getTime()-fmdiff.getTime())/oneday);

dtecurr is a string. Just because it is formatted like a user-friendly date, doesn't mean it's an instance of Date.

I think you want
var dtecurr=new Date(curdtedy + "-" + curdtemh + "-" + curdteyr);
instead of
var dtecurr=(curdtedy "-" curdtemh "-" curdteyr);

That is because dtecurr is not a Date object here.
Make it a Date object:
var dtecurr = new Date();
And then set values by methods setDate(), setMonth() and setFullYear()

Related

replace the string from just before the last dot(.) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string like A/B/C/D.txt
So I want to replace the character just before the last . with the new one.
Like I would add E in last So the output should be A/B/C/E.txt
Can this is possible with only replace and concat Or Regex should help?
I'm sure the output is correct.
A/B/C/E.txt
var filename = 'A/B/C/D.txt';
var dotIndex = filename.lastIndexOf('.');
document.write(filename.substr(0, dotIndex-1) + 'E'+filename.substr(dotIndex, filename.length-dotIndex));
var parts = 'A/B/C/D.txt'.split('.');
if(parts.length > 1)
{
parts[parts.length-2] = parts[parts.length-2].replace(/.$/,"E");
let result = parts.join('.');
console.log(result);
}
Try this:
let name = "A/B/C/D.txt"
let res = name.substr(0, name.lastIndexOf(".")) + "E." + name.substr(name.lastIndexOf(".") + 1);
console.log(res);

Enter age javascript error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Can anyone tell me what's wrong with this specific code and how can I fix it? Thanks in advance.
<script language=javascript type="text/javascript">
toDays(21);
function toDays(years)
{
var time;
time = 365*years;
return time;
}
document.write("My age is " + time);
</script>
Document.write should be inside function or toDays(21) outside function
function toDays(years)
{
var time;
time = 365*years;
document.write("My age is " + time);
return time;
}
document.write("My age is " + toDays(21));
http://codepen.io/nagasai/pen/oLaRdw
The return value from the function was not stored in a variable.
Due to hoisting of the function in Javascript, the toDays is defined before its defination line.
Store the result from the function in a global variable time.
Use document.write to print the results.
var time = toDays(21);
function toDays(years)
{
var time;
time = 365*years;
return time;
}
document.write("My age is " + time);

error with javascript: missing ; before statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm getting an error with my javascript: "missing ; before statement".
I'm trying to read in a date, add 6 months onto the date if it meets a certain criteria ( joiner type in this case) and if not just return that date.
I can't see whats wrong here, it must be something small, any ideas??
Thanks!
function checkenddate(Par) {
var array = Par.split("!!");
var usermskey = array[0];
var date = array[1];
var joinertype = array[2];
saprep = UserFunc.uGetConstant("glb.REPOSITORY_ECC");
attr1 = "Z_VALIDTO" + saprep;
uWarning("Attribute: " + attr1);
if (date == null && joinertype.equals("Contractor"))
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calender c = Calender.getInstance();
c.setTime(sdf.parse(date));
c.add(Calender.MONTH, 6);
enddate = sdf.format(c.getTime());
uWarning("End Date:" + enddate);
OutString = uIS_SetValue(usermskey, 0, attr1, enddate);
return enddate;
} else {
OutString = uIS_SetValue(usermskey, 0, attr1, date);
return date;
}
}
Thats not valid javascript. You can't have typed variables such as SimpleDateFormat sdf = new blah(). Change your types to var and it will work as expected.
var sdf = new SimpleDateFormat("yyyy-MM-dd");
var c = Calender.getInstance();
This is not how you declare a JS variable:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
You need this instead:
var sdf = new SimpleDateFormat("yyyy-MM-dd");
I would recommend using one of the linting tools online (e.g. JSHint or JSLint) to help track down these issues - very handy.

Get values of Input[type=(type)] from specific class with jQuery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have this code that does not work:
kw _class = _keyword1;
var text = $("'input." + kw_class + "[type=text]'").val();
var val = $("'input." + kw_class + "[type=hidden]'").val();
Firefox console comes out with this:
`Syntax error, unrecognized expression: 'input._keyword1[type=text]'
I have tried at least three combos of this that are not working that I found from other questions.
Yes because you have ' ' inside of the selector. It should be:
var text = $("input." + kw_class + "[type=text]").val();
var val = $("input." + kw_class + "[type=hidden]").val();
you have extra '' in the selector
var text = $('input.' + kw_class + '[type=text]').val();
var val = $('input.' + kw_class + '[type=hidden]').val();

How to convert string to date javascript?

I have this as my date: 1212009 so this should be like 12/1/2009 but I am stuck this as an id field and id fields cannot have dashes in there names.
Now can java-script take this number and re add the slashed in for me or how would I go about doing this?
Thanks
You should have two-digit numbers for the month and day, since "1212009" is ambiguous, could be interpreted as "1/21/2009" or "12/1/2009".
If you add the leading zeros, you know that there will be always 8 digits, so you can do something like this:
var str = "12012009"
var result = str.replace(/(\d{2})(\d{2})(\d{4})/, "$1/$2/$3");
// 12/01/2009
Or
var result = str.match(/(\d{2})(\d{2})(\d{4})/).slice(1).join('/');
// 12/01/2009
Well without leading zeros, it's not possible to make a reliable conversion. It's impossible to know if 1232009 is 1/23/2009 or 12/3/2009 without leading zeros. Also, your example number could be interpreted as 1/21/2009 too.
All you need to do is pass the string into the new date constructor.
Here is a good javascript resource for you to look at.
http://www.javascriptkit.com/jsref/date.shtml
Assuming that you add the leading zeroes to remove the ambiguity, you can format the date like so:
dateString = String(id).match(/(\d{2})(\d{2})(\d{4})/).slice(1).join('/')
Maybe this will help you to get rid of the six or seven numbers long date (most important part is the first part):
var sDate = "1212009";
if(sDate.length == 7){
sDate = sDate.substr(0,2) + "0" + sDate.substr(2);
}
else if(sDate.length == 6){
sDate = "0" + sDate.substr(0,1) + "0" + sDate.substr(1);
}
// This part can be done with Regular Expressions, as shown in other comments
var month = parseInt(sDate.substring(0,2));
var day = parseInt(sDate.substring(2,4));
var year = parseInt(sDate.substring(4));
alert(day + "/" + month + "/" year);
// Will alert 12/1/2009
However,
1232009 will always be 01/23/2009, not 12/03/2009
112009 changes to 1/1/2009
10102009 changes to 10/10/2009

Categories