add a day in date using jquery

Posted by Klbaiju under jQuery on 5/11/2014 | Points: 10 | Views : 2869 | Status : [Member] | Replies : 3
Hi,following is working code.my requirement is i want add days to date using jquery

var day;//sample data 3

var dateA;
dateA = hval.split("-"); //sample data 100-20-may-2014
var ida = dateA[0] + '-' + dateA[1] + '-' + dateA[2] + '-' + dateA[3];
var it = eval(dateA[1]);
for (var i = 1; i <= day; ++i)
{
ida = dateA[0] + '-' + it + '-' + dateA[2] + '-' + dateA[3];
$('#' + ida).parent().empty().append("<a class='anchor3' id ='" + ida + "' href='#' >booked </a>").css('background-color', 'LightGrey');
it = eval(it) + 1;//adding day
}

output

ida=100-20-may-2014 booked

ida=100-21-may-2014 booked

ida=100-22-may-2014 booked

here sample data is day=3 and hval=100-20-may-2014.this is working fine.

if day=3 and hval=100-30-may-2014,this will not work fine

as iam getting output as

ida=100-30-may-2014 booked

ida=100-31-may-2014 booked

ida=100-32-may-2014 booked

error is ida=100-32-may-2014 booked should be ida=100-01-jun-2014 booked

requirement is

dateA = hval.split("-"); //sample data 100-20-may-2014
var ida = dateA[0] + '-' + dateA[1] + '-' + dateA[2] + '-' + dateA[3];

dateA[1] + '-' + dateA[2] + '-' + dateA[3]; this part is date ie 20-may-2014.in the code there is adding a day by simply add a number.instead of that

dateA[1] + '-' + dateA[2] + '-' + dateA[3] this part should be declared as date.

how to solve this

Regards

Baiju




Responses

Posted by: kgovindarao523-21772 on: 5/12/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Try like this:


dateA = hval.split("-");
var days=dateA[0];
var date = new Date(dateA[3], dateA[2], dateA[1]);
var newdate = new Date(date);
newdate.setDate(newdate.getDate() - days);
var modifiedDt = new Date(newdate);

now you can use modifiedDt

Thank you,
Govind

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 5/12/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Baiju,

To solve you question, some logic is involved.

Just run this below code and you will get your required output. Also I have included a JQuery file to solve your problem.

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>

<script src="jquery-1.11.1.min.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">

$(document).ready(function() {

var day = 3; //sample data 3
//var hval = "100 - 20 - 4 - 2014";
var hval = "100-30-may-2014";
var dateA;
dateA = hval.split("-"); //sample data 100-20-may-2014
var ida = dateA[0] + '-' + dateA[1] + '-' + dateA[2] + '-' + dateA[3];
var it = eval(dateA[1]);
var date = new Date(dateA[3], returnMonthNumber(dateA[2]), dateA[1]);
for (var i = 1; i <= day; ++i) {
ida = dateA[0] + '-' + paddy(date.getDate(), 2) + '-' + returnMonthName(date.getMonth()) + '-' + date.getFullYear();
//alert(ida);
$('#' + ida).parent().empty().append("<a class='anchor3' id ='" + ida + "' href='#' >booked </a>").css('background-color', 'LightGrey');
date.setDate(date.getDate() + 1);
}
});


function returnMonthNumber(month) {
if (month.toLowerCase() == "january") { return 0 };
if (month.toLowerCase() == "february") { return 1 };
if (month.toLowerCase() == "march") { return 2 };
if (month.toLowerCase() == "april") { return 3 };
if (month.toLowerCase() == "may") { return 4 };
if (month.toLowerCase() == "june") { return 5 };
if (month.toLowerCase() == "july") { return 6 };
if (month.toLowerCase() == "august") { return 7 };
if (month.toLowerCase() == "spetember") { return 8 };
if (month.toLowerCase() == "october") { return 9 };
if (month.toLowerCase() == "november") { return 10 };
if (month.toLowerCase() == "december") { return 11 };
}

function returnMonthName(monthNumber) {
if (monthNumber == 0) { return "january" };
if (monthNumber == 1) { return "february" };
if (monthNumber == 2) { return "march" };
if (monthNumber == 3) { return "april" };
if (monthNumber == 4) { return "may" };
if (monthNumber == 5) { return "june" };
if (monthNumber == 6) { return "july" };
if (monthNumber == 7) { return "august" };
if (monthNumber == 8) { return "spetember" };
if (monthNumber == 9) { return "october" };
if (monthNumber == 10) { return "november" };
if (monthNumber == 11) { return "december" };
}

function paddy(n, p, c) {
var pad_char = typeof c !== 'undefined' ? c : '0';
var pad = new Array(1 + p).join(pad_char);
return (pad + n).slice(-pad.length);
}

</script>

</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>


Happy coding.

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 5/15/2014 [Member] Starter | Points: 25

Up
0
Down
Hi friend thanks for your reply.
your code is working at some stages.only working at month of may after changing like below
i have changed your code like this means avoid lowercase and changed firstletter capital like this
function returnMonthNumber(month) {
if (month == "January") { return 0 };
if (month == "February") { return 1 };
if (month == "March") { return 2 };
if (month == "April") { return 3 };
if (month == "May") { return 4 };
if (month=="June") { return 5 };
if (month == "July") { return 6 };
if (month == "August") { return 7 };
if (month == "September") { return 8 };
if (month == "October") { return 9 };
if (month == "November") { return 10 };
if (month == "December") { return 11 };
}

function returnMonthName(monthNumber) {
if (monthNumber == 0) { return "January" };
if (monthNumber == 1) { return "February" };
if (monthNumber == 2) { return "March" };
if (monthNumber == 3) { return "April" };
if (monthNumber == 4) { return "May" };
if (monthNumber == 5) { return "june" };
if (monthNumber == 6) { return "July" };
if (monthNumber == 7) { return "August" };
if (monthNumber == 8) { return "September" };
if (monthNumber == 9) { return "October" };
if (monthNumber == 10) { return "November" };
if (monthNumber == 11) { return "December" };
}
function paddy(n, p, c) {
var pad_char = typeof c !== 'undefined' ? c : '0';
var pad = new Array(1 + p).join(pad_char);
return (pad + n).slice(-pad.length);
}
yur code is woking fine only the month of 2014 may.rest of month is not working.
i need only from 2014 may
how to solve this
Regards
Baiju

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response