Forum Moderators: open

Message Too Old, No Replies

Anyway to modify a date script

To always display 15th or end of month?

         

johnnydequino

11:47 am on Jun 28, 2004 (gmt 0)

10+ Year Member



Is there anyway to modify a javascript date query to always show bi-monthly dates?

For example, if today was June 5th, can this date script show June 15th? After the 15th, say like the 17th, can this script show the end of month date?

Not sure if something like this exists?

Thanks -

jd

Bernard Marx

12:32 pm on Jun 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This one seems to work, and should handle Feb/leap years.
Wait to see if something better comes along though.


var today = new Date()
var date = today.getDate()

if(date>=15)
{
today.setMonth(today.getMonth()+1 % 12)
// this next doesn't quite fit, I know
// It goes back 1 day to the end of the prev month
today.setDate(0)
}
alert(today)

johnnydequino

2:15 pm on Jun 28, 2004 (gmt 0)

10+ Year Member



Thanks for your help Bernard - how about this:

Anyway to format the date to MM/DD/YYYY format? This is your script printed instead of alerted.

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!-- //Begin
var today = new Date()
var date = today.getDate()
if(today>=15)
{
today.setMonth(today.getMonth()+1 % 12)
today.setDate(0)}
document.write(" "+today+" ")
//End -->
</SCRIPT>

Bernard Marx

2:35 pm on Jun 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The Date object isn't one of my strong points (I had to dig around & experiment for the last post). There may well be a method that you can apply to today, perhaps even with arguments, to get out dd/mm/yyyy. However, I can't find it, so I'm offering this. It uses a custom Number utility method, prefix, that's useful to have around [put that part anywhere at the top of your code (not inside a function)]

/* Number method. rtn: String */
Number.prototype.prefix = function(n){
return new String(Math.pow(10,n)+this).substring(1)
}
/*-----------*/

..code as before..

document.write
(
today.getDay().prefix(2) +"/"
+ today.getMonth().prefix(2) +"/"
+ today.getFullYear()
)