Liza
Liza

Reputation: 19

How to Add Two Days from Current Date to the Value of a Hidden Input

This is my code and I want to add two days from the current date to the value of a hidden input. If I borrow now, this results in a waiting period of two days. It will be better if I borrow on Friday; Saturdays and Sundays will not count so the waiting period ends on Monday, four days later.

<input type="hidden" name="due_date" id="sd" maxlength="10" style="border: 3px double #CCCCCC;" required/>

Upvotes: 0

Views: 171

Answers (3)

slevy1
slevy1

Reputation: 3820

The code creates an array of objects referring to the days of the week as well as a Date object oDate used to retrieve the current date information. If the day of the week is not Friday, then user is advised to wait till Friday.

The hidden input "due_date" has its value set to two days from the current date unless that day is Friday in which case the due date becomes 4 days later, to skip the weekend and add the usual 2 days to the waiting period. If the hidden input were part of a form, once it is submitted, and the data validated, assuming submission by POST, one could use variable $_POST["due_date"] in an INSERT query to store that value in a database, making sure to use either mysqli_real_escape_string() or PDO and bound parameters.

Note: I altered the HTML so that both the NAME and ID attributes of the hidden input are both set to "due_date".

var d = document;
d.g = d.getElementById;

var arrDaysOfWeek = {"Sunday":0,"Monday":1,"Tuesday":2,"Wednesday":3,"Thursday":4,"Friday":5,"Saturday":6};

var arrWkDayNames = Object.keys( arrDaysOfWeek );

var oDate = new Date();
var currDay = oDate.getDay();

var md = oDate.getDate();
var mm = oDate.getMonth() + 1;
var y =  oDate.getFullYear();


var waitPeriod = 2;  // default

var daysTillFriday = (currDay == 0)? arrDaysOfWeek["Friday"]
: arrDaysOfWeek["Friday"] - currDay;

if (currDay == arrDaysOfWeek["Saturday"]) {
   daysTillFriday = arrWeekDayNames.length + arrDaysOfWeek["Friday"] - currDay;
}

var mess = "";

if (currDay != arrDaysOfWeek["Friday"] ) {
    mess = "\nYou should wait to borrow on Friday, i.e. " + daysTillFriday + " days from today.";
}

if( currDay + 2 != arrDaysOfWeek["Friday"] ) {
 daysTillFriday = arrDaysOfWeek["Friday"] - currDay - 2;
 
  mess += "\nSo, best not even in two days. Just wait till Friday which will be in " + daysTillFriday  + " days from two days from now.";
  
}

waitPeriod = (currDay == arrDaysOfWeek["Friday"] )
? 4 //skip sat. & sun. plus 2
: 2; // usual wait period


oDate.setDate(md + waitPeriod);


mess += "\nTo proceed know that the happening date is " + oDate;

//USA date style ...
var date_parts = [ mm, md, y ];

mess += "\nToday is " + arrWkDayNames[ currDay ] + ", " + date_parts.join("/");

d.g("display").textContent = mess;

d.g("due_date").value = oDate;
console.log( "Hidden input due date value: " +  d.g("due_date").value );
<div id="display"></div>

<input type="hidden" name="due_date" id="due_date" maxlength="10" style="border: 3px double #CCCCCC;" required/>

Upvotes: 0

Prateek
Prateek

Reputation: 1247

You can use JavaScript to add 2 days and For Friday(5) add 4 days to skip Saturday And Sunday plus 2 days:

var currentDate = new Date();

//Checking If Current day is Friday

if(currentDate.getDay() == 5) {
    var numberOfDaysToAdd = 4; //Adding 4 to skip sat. & sun. if Friday
} else {
    var numberOfDaysToAdd = 2; //Adding 2 days if not Friday
}
currentDate.setDate(currentDate.getDate() + numberOfDaysToAdd);

//Formatting to dd/mm/yyyy :

var dd = currentDate.getDate();
var mm = currentDate.getMonth() + 1;
var y = currentDate.getFullYear();

var someFormattedDate = dd + '/'+ mm + '/'+ y;

// Displaying Fromatted Date

document.getElementById("display").innerHTML = someFormattedDate;
<div id="display"></div>

It has been assumed that nothing is Borrowed on Saturday And Sunday.

Upvotes: 1

Shafikul Islam
Shafikul Islam

Reputation: 359

You can use JavaScript, no jQuery required:

var someDate = new Date();
var numberOfDaysToAdd = 2;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd); 

Formatting to dd/mm/yyyy :

var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();

var someFormattedDate = dd + '/'+ mm + '/'+ y;

answer from - How to add number of days to today's date?

You can do it in php

echo date('Y-m-d', strtotime("+2 days"));

Answer From - Add number of days to a date

Upvotes: 0

Related Questions