Im in the land of JavaScript and I'm trying some functions for my university project, but I got stuck in this part:
function validateDate(){
alert("validateDate");
var date = document.getElementById("dateN");
var yearN = data.getFullYear();
alert(yearN.value);
var dateA = new Date();
var yearA = dateA.getFullYear`enter code here`();
alert(yearA.value);
if(((yearA - yearN)<18) || ((yearA - yearN)>120)){
alert("age between 18 and 120 only.");
data.style.backgroundColor = "red";
}
}
When I try to print the values, nothing happens, Which terrible wrong thing Im doing here guys?
The dateN is comes from another part (which is working =D ).
Sorry if its a very "noob" question,
Thanks in advance!
There is two mistake here :
var date = document.getElementById("dateN");
var yearN = data.getFullYear();
date is refering a DOM element an it is not a date !
data is undefined !
maybe you want make a Date with the value from "dateN" ?
so according to the HTMLElement you have to get his value and create a new Date !
// saying is a input text and a valid date format !
var date = new Date( document.getElementById("dateN").value );
var yearA = dateA.getFullYear`enter code here`();
// ^
// this is not valid o----------------|
You are calling data.getFullYear() -
var yearN = data.getFullYear();
Don't you mean date.getFullYear??
Related
I am using a report parameter in BIRT.
It a string, which contains the month/year, like: 08/2018
To test the value, I am using the following code. It is located in a Dynamic Text:
var dateStringArray = params["monthYear"].value.split("/");
var date = new Date(parseInt(dateStringArray[1]), parseInt(dateStringArray[0]) - 1, 1);
var t = parseInt(dateStringArray[0]);
t;
If I fill the parameter with 08/2018, I get a NaN, see:
But if I fill the parameter with 07/2018, it is working correctly:
I have tested it with several numbers. It is just not working with 08 and 09. All other number til 10 are working...
This seems to be a weird scenario. Need to raise a bug on this.
But for your workaround, you can make use of the code below in Dynamic Text which is working fine:
var dateStringArray = params["monthYear"].value.split("/");
var monNum;
if (BirtStr.charLength(dateStringArray[0]) == 2)
{monNum = BirtStr.right(dateStringArray[0],1);}
else {monNum = dateStringArray[0];}
monNum;
//var date = new Date(parseInt(dateStringArray[1]), parseInt(dateStringArray[0]) - 1, 1);
var t = parseInt(monNum);
t;
I need to find current EST offset value using javascript without any server side code
Because I am hardcoding Offset value in my javascript file,i need to pass dynamically because of Day Light Saving.
Kindly Help me on that
Thanks in advance..,
Using moment-timezone:
var currentUSEasternTime = moment.tz('America/New_York');
var minutesOffset = currentUSEasternTime.utcOffset(); // -240
var hoursOffset = minutesOffset / 60; // -4
var offsetString1 = currentUSEasternTime.format('Z'); // "-04:00"
var offsetString2 = currentUSEasternTime.format('ZZ'); // "-0400"
Not sure what output you were looking for, so gave you a few options.
Here are two angular2 custom validations that I wrote, the first one validateAge works, but the second one validateDob does not ... the difference is the validateAge uses the component that I am on and is a text based field, the second one needs to use a Date Entry field and find the difference between today's date and the birthdate to find the actual age and then measure it against the age field. but something is not right ... any ideas
function validateAge(control: FormControl): { [s: string]: boolean } {
if (parseInt(control.value) <= 0) {
return {invalidAge: true};
}
}
function validateDob(control: FormControl): {[s:string]: boolean}{
var today = new Date();
var calcAge = today - control.value;
if (calcAge != parseInt([{age}]) ){
return {invalidDate: true}
}
}
The issue you have here is that your control.value is not a Date object, but rather the string representation.
var today = new Date();
Difference in milliseconds between the current timestamp and the entered value
var diff = today - new Date(control.value);
divide by ms per year and take the floor
var calcAge = Math.floor(diff/ (1000*60*60*24*365)));
Now do whatever comparison you need against the appropriate value. You didn't show us what your age object is so I don't actually know what comparison you're looking for.
if (calcAge < someAgeThreshold) ){
return {invalidDate: true}
} else {
return null;
}
Also note that with custom validation the validator returns no error when you return null and anything with a value is considered to have an error.
I am trying to calculate how old a person is with jQuery. I tried using this code but I am not getting any results. I assume that I cannot but a varible in date(). What could I do to make this work?
<p id="age">2015/01/01</p>
var ptag = $('#age');
var birthdate = new Date(ptag);
var cur = new Date();
var diff = cur-birthdate;
var age = Math.floor(diff/31536000000);
alert(age);
You need to get the contents of the #age element, not the element itself.
var ptag = $('#age').text();
var age = new Date() - new Date($('#age').text());
age /= 31536000000;
$(selector).text() will return the content of a DOM element as a text.
And since we do not want to use a bunch of variables, we simply define one, as the difference of two dates: the 'now' minus the one from the text.
And since the above is in miliseconds, we devied it... and that will return age in years.
jQuery .text()
I have an .innerHTML statement that isn't working correctly, but only while within an if statement.
document.getElementById("music").innerHTML = "[MUSICPLAYERCODE]";
is the code that I'm using, and it's within this.
<script type="text/javascript">
var thedate - new Date();
var hourofday = thedate.getHours();
if(hourofday == 0) {
document.getElementById("music").innerHTML = "[MUSICPLAYERCODE]";
}
</script>
While outside of the if statement, the code works completely fine, however, inside the if statement it refuses to work. Even if I change the condition to just a plain true, the code still will not execute. I'm new to javascript, and I can't seem to find any error after looking for a couple hours. If it helps at all, the music player I'm using is at Billy Tumblr Audio Player.
I think you have added not just the if condition but also the code related to date calculation.
There you have a syntax error, used - instead of =. In your browser console you should see an error like Uncaught SyntaxError: Unexpected token -
var thedate = new Date();
// ^ = not -
var hourofday = thedate.getHours();
if (hourofday == 0) {
document.getElementById("music").innerHTML = "[MUSICPLAYERCODE]";
}
var thedate - new Date(); ?
Change it to:
var thedate = new Date();
You can try like this.
window.onload=function(){
var thedate = new Date();
var hourofday = thedate.getHours();
if(hourofday == 0) {
document.getElementById("music").innerHTML = "<H1>[MUSICPLAYERCODE]</H1>";
}
};