/    Sign up×
Community /Pin to ProfileBookmark

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 ?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@CharlesApr 13.2005 — <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>
Copy linkTweet thisAlerts:
@Warren86Apr 13.2005 — "European" format:

<HTML>

<Head>

<Script Language=JavaScript>

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;
}
}


</Script>

</Head>

<Body>

<Form name='Form1'>

Some Date: (dd/mm/yyyy) <input type=text size=9 name='inDate' onblur="calcDate(this)"><br>

Computed Date: <input type=text size=8 name='backDate' readonly>

</Form>

</Body>

</HTML>

"American" format:

<HTML>

<Head>

<Script Language=JavaScript>

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;
}
}


</Script>

</Head>

<Body>

<Form name='Form1'>

Some Date: (mm/dd/yyyy) <input type=text size=9 name='inDate' onblur="calcDate(this)"><br>

Computed Date: <input type=text size=8 name='backDate' readonly>

</Form>

</Body>

</HTML>

----------- Unsubscribing from this thread...
Copy linkTweet thisAlerts:
@JuuitchanApr 13.2005 — If you try to get the "zeroth" day of a month, JavaScript will give you the last day of the preceding month.
Copy linkTweet thisAlerts:
@JuuitchanApr 13.2005 — Try this example:

<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
Copy linkTweet thisAlerts:
@CharlesApr 13.2005 — If you try to get the "zeroth" day of a month, JavaScript will give you the last day of the preceding month.[/QUOTE]Which is how my example, above,works.
Copy linkTweet thisAlerts:
@JuuitchanApr 14.2005 — That's not what I saw. Please re-examine your code, Charles.
×

Success!

Help @fvnnmike spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...