i am developing a function that accept a date as a date string and return the date as a string of the last day of the previous month.
does someonhe has any idea? tha ?
<script type="text/javascript"> <!-- Date.prototype.getFirstDayOfPreviousMonth = function () {return new Date (this.getFullYear (), this.getMonth () - 1, 1)} date = new Date ('13 April 2005'); then = date.getFirstDayOfPreviousMonth(); alert (then.toDateString()); // --> </script>
function calcDate(isField){ isValid = true; splitDate = isField.value.split("/"); refDate = new Date(splitDate[1]+"/"+splitDate[0]+"/"+splitDate[2]); if (splitDate[1] < 1 || splitDate[1] > 12 || refDate.getDate() != splitDate[0] || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))) { alert('Invalid date'); isField.value = ""; isField.focus(); isValid = false; } if (isValid) { dateStr = new Date(Date.parse(refDate)-refDate.getDate()*86400000); slashStr = dateStr.getDate()+"/"+(dateStr.getMonth()+1)+"/"+dateStr.getFullYear(); document.forms.Form1.backDate.value = slashStr; } }
function calcDate(isField){ isValid = true; splitDate = isField.value.split("/"); refDate = new Date(isField.value); if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))) { alert('Invalid date'); isField.value = ""; isField.focus(); isValid = false; } if (isValid) { dateStr = new Date(Date.parse(refDate)-refDate.getDate()*86400000); slashStr = dateStr.getMonth()+1+"/"+dateStr.getDate()+"/"+dateStr.getFullYear(); document.forms.Form1.backDate.value = slashStr; } }
<i> </i>// JavaScript month numbering is crazy. // In JavaScript, month 6 = July. alert(new Date(2000,6,4)) // gives July 4, 2000 alert(new Date(2000,6,0)) // gives June 30, 2000
If you try to get the "zeroth" day of a month, JavaScript will give you the last day of the preceding month.[/QUOTE]
Help @fvnnmike spread the word by sharing this article on Twitter...