Fix days of month depending on particular year:
There are some cases where we have two dropdowns(for month and days) and upon selecting month days dropdown needs to be update so i wrote a few lins of code for it and hope it helps anyone face this situation
This is the method which actually returns the number of days available for a month and year:
correctDaysInMonth = 32 - new Date(year, month, 32).getDate();
function which adjust the nmber of days dropdown upon selecting year dropdown :
function AdjustDaysOfTheMonth(controlID, month, year) {
//Calculate exact number of day's in a month
//when we pass the parameter days greater than the particular month available days
// It throws the actual number of days available
correctDaysInMonth = 32 - new Date(year, month, 32).getDate();
console.log('exact Dates : ' + controlID + ' : ' + +correctDaysInMonth);
var maxDayValue = Number($(controlID + " option:last-child").val());
if (maxDayValue != correctDaysInMonth) {
// If maximum value of the days dropdown is less than the
// actual days add additional days
if (maxDayValue < correctDaysInMonth) {
//Add element from the range of month
for (var count = maxDayValue + 1; count <= correctDaysInMonth; count++) {
//Create option element
var dayElementOption = document.createElement('option');
dayElementOption.text = count;
dayElementOption.value = count;
//Add it to control
try {
$(controlID)[0].add(dayElementOption, null); // Fail in IE
}
catch (ex) {
$(controlID)[0].add(dayElementOption); //work in IE only
}
}
}
else {
//Remove element not in the range of month
for (var count = maxDayValue + 1; count > correctDaysInMonth; count--) {
$(controlID)[0].remove(count);
}
}
}
}
Wednesday, March 28, 2012
Javascript Currcncy formatter
// Used to format the currency amount
function formatCurrencyAmount(amount) {
//replace non numeric value. can add more if any
amount= amount.toString().replace(/\$|\,/g, "");
if (isNaN(amount)) amount= "0";
sign = (amount== (amount= Math.abs(amount)));
amount= Math.floor(amount* 100 + 0.50000000001);
cents = amount% 100;
amount= Math.floor(amount/ 100).toString();
if (cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((amount.length - (1 + i)) / 3); i++)
amount= amount.substring(0, amount.length - (4 * i + 3)) + "," + amount.substring(amount.length - (4 * i + 3));
//Return it with the $ amount
return (((sign) ? "" : "-") + "$" + amount+ "." + cents);
}
Cheers,
function formatCurrencyAmount(amount) {
//replace non numeric value. can add more if any
amount= amount.toString().replace(/\$|\,/g, "");
if (isNaN(amount)) amount= "0";
sign = (amount== (amount= Math.abs(amount)));
amount= Math.floor(amount* 100 + 0.50000000001);
cents = amount% 100;
amount= Math.floor(amount/ 100).toString();
if (cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((amount.length - (1 + i)) / 3); i++)
amount= amount.substring(0, amount.length - (4 * i + 3)) + "," + amount.substring(amount.length - (4 * i + 3));
//Return it with the $ amount
return (((sign) ? "" : "-") + "$" + amount+ "." + cents);
}
Cheers,
Subscribe to:
Posts (Atom)