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()
Related
Specific situation.. I'm having an array filled with datetimes I pull in via an api.
Users should be able to select a date from a datepicker (only showing dates available in the array) and afterwards see the corresponding time.
So what I've done..
The original array is obtained via php, so before starting to populate the datepicker with possible dates I create an extra array with dates only.
Since I maintain the key's it's possible to put these 2 arrays next to eachother.
Array looks as following:
["8-8-2017,07:00", "26-8-2017,07:00"];
So far so good...
After a user picks a date I trigger this to be able to start digging for the time corresponding that date.
Now it's getting messy...
$('#datepick').datepicker().on("input change", function(e) {
$("#uur").text('');
var selecteddate = e.target.value;
var searchArr = datesArray;
var ind = searchArr.indexOf(selecteddate.toString());
var result = datesArray.filter(function(item) {
return typeof item == 'string' && item.indexOf(selecteddate.toString()) > -1;
});
var afterComma = result.toString().substr(result.toString().indexOf(",") + 1);
var final = afterComma.replace(":", "u");
$("#uur").text("De warming up party gaat van start rond " + final);
});
The result is that this only works on the last element of the array.
Because I'm splitting based on the comma's. Now I know the easiest way to work arround this would be to change the , that's seperating date and time in another symbol but still I'm wondering why this couldn't be easier.
You convert whole array to string every time. You should change following code:
var afterComma = result.toString().substr(result.toString().indexOf(",") + 1);
To this;
var afterComma = item.toString().substr(item.toString().indexOf(",") + 1);
Edit:
I also missed the loop above
//for every item in result, afterComma will refer to related minute string
for (var item in result) {
var afterComma = item.toString().substr(item.toString().indexOf(",") + 1);
// Do rest here
}
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.
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??
I have a question, in the code below if I declare the start_date & end_date variables before $.each the last part which inserts some span tags doesn't work, but if they are declared after $.each then it does.
Why is this? I cannot figure it out and I have no ideas at all.
var dates_para = $("#field_dates p");
dates_para.each(function(){
var html_content = $(this).html();
$(this).html(html_content.replace("–", ""));
});
var start_date = $("#start_date");
var end_date = $("#end_date");
start_date.after("<span id='start_datepicker' class='datepicker'></span>");
end_date.after("<span id='end_datepicker' class='datepicker'></span>");
If the #start_date and #end_date elements are parts of the paragraphs, then writing new html to the paragraphs will create new elements and the old references are invalidated.
I'd like to create a javascript timestamp based on a rails date_select and time_select property. I attached an onChange function to the select helper and fetching the innerhtml to read the values into a div which works fine. Now I want to use those strings from the select property and create a timestamp in js (using it for validations).
I did first try this by making integers from the innerhtml values:
function insertText10()
{
var start_day = document.new_link['link[start_at(3i)]'];
var start_month = document.new_link['link[start_at(2i)]'];
var start_year = document.new_link['link[start_at(1i)]'];
var start_hour = document.new_link['link[start_at(4i)]'];
var start_minute = document.new_link['link[start_at(5i)]'];
var selOption1 = start_day[start_day.selectedIndex];
var selOption2 = start_month[start_month.selectedIndex];
var selOption3 = start_year[start_year.selectedIndex];
var selOption4 = start_hour[start_hour.selectedIndex];
var selOption5 = start_minute[start_minute.selectedIndex];
start_date = new Date(parseInt(selOption3.innerHTML),parseInt(selOption2.innerHTML),parseInt(selOption1.innerHTML),parseInt(selOption4.innerHTML),parseInt(selOption5.innerHTML),0,0);
then by using strings:
start_date = new Date(selOption3.innerHTML+selOption2.innerHTML+selOption1.innerHTML+selOption4.innerHTML+selOption5.innerHTML);
but neither works.
What am I doing wrong?
--
PS: I checked the w3s docu http://www.w3schools.com/jsref/jsref_obj_date.asp to find the solution above.
Solved:
start_date = new Date(parseInt(selOption3.value),parseInt(selOption2.value),parseInt(selOption1.value),parseInt(selOption4.value),parseInt(selOption5.value),0,0);