Greetings, I have these 2 variables: var numero, fecha
where number contains an integer and date, well a date 01/12/1990
. What would be the procedure to follow in JAVASCRIPT?
It should be noted that the questions that are in the stack obtain the dates with new date()
, but here the date is already inside the date variable. I tried placing inside date($("#fecha"))
and it didn't work.
NOTE: With ajax I render the view and this data is stored in 2 spans, EJ Thanks.
date($("#fech"))
It doesn't work for you because the syntax isDate()
(initial case) insteaddate()
and it$("#fech")
returns the HTML element withid="fech"
but Date() can't process an HTML element, it must be passed a value.Assuming that
fech
it is a date type input element, the way to obtain the data using jQuery is as follows:And to add days it would be as follows
NOTE: If the value of the date is contained in a
<span>
, instead ofval()
we usetext()
and the code to use would be as follows:Well, to add days to a date you have to collect those days from somewhere, in this case you can do it from an input, then you assign those days to the date you want to add. Once added we have the date, then we just show the date with a bit of formatting. It's all set.
It is important to take into account the time zone, if you do it wrong, it will add the days but when you do
setDate()
it, the result may not be as expected.This is the solution that worked for me: