How to add days to shown current date (display date in future).

Joined
Jan 9, 2025
Messages
1
Reaction score
0
Can someone tell me how to add 46-days to the HTML under that shows the current date?
So instead of 9 Jan 2025 (today) its should show 24 Feb 2025. Many thanks in advace? I'm just a beginner at coding.

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--

function initArray() {
this.length = initArray.arguments.length;
for (var i = 0; i < this.length; i++)
this[i+1] = initArray.arguments;
}

var dagArray = new initArray("zondag","maandag", "dinsdag","woensdag","donderdag","vrijdag","zaterdag");

var maandArray = new initArray("januari","februari","maart","april","mei","juni","juli", "augustus","september","oktober","november","december");

var nu = new Date();
var dagtekst = dagArray[(nu.getDay()+1)];
var dag = nu.getDate();
var maandtekst = maandArray[(nu.getMonth()+1)];
var jaar = nu.getYear();
var jaar4 = ((jaar < 1900) ? (jaar + 1900) : (jaar));

var datumweergave = "Eerste mogelijkheid ná: " + dagtekst + " " + dag + " " + maandtekst + " " + jaar4;

document.write(datumweergave);

//-->
</SCRIPT>
 
Joined
Sep 21, 2022
Messages
216
Reaction score
32
There are 2 ways to do that.

The first is to write two functions.

Function A() converts a date in day;month;year format into the number of days from some fixed point in the past.

Function B() does the reverse of function A().

Convert today's date into a number with A(), add 46 to it, convert the sum back to a date with B().

The second way is to look in your javascript manual. This type of thing is very common and I would expect the language I'm using to have the functions already coded in a standard library.
 
Joined
Jul 4, 2023
Messages
570
Reaction score
76
The second way is to look in your javascript manual.
I definitely agree, and in this manual you can find the setDate() method, IMO, which simplifies adding days to a date as much as possible.
Check this out:
HTML:
<script>
    // Today's date
    const today = new Date();
    // Number of days to add
    const days_to_add = 46;
    // Adding days
    const future = new Date();
    future.setDate(today.getDate() + days_to_add);

    document.write(`Today's date: ${today.toISOString().split('T')[0]}<br>`);
    document.write(`Date in ${days_to_add} days: ${future.toISOString().split('T')[0]}`);
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top