Take the 2-minute tour ×

I have the following scenario that I am strugling to code.

I have a valuation date that is a string that is chosen by a user from a calander popup. What I need to do is take that date and pass it into a function that works outs a second date depending on the value of that date. If the first date is more than 7 days from the first day of the month use the first day of the month else use the last day of the month. This needs to happen in client side as this date need to be displayed after they have chosen the first date.

SO far I have the below:

Function CompareDate()
{ var date1 = document.getElementById("textbox1");
var x = new date();
var year = x.getYear();
var day = x.getDay();
var thisMonthFirstDay = new Date(year, month,1)
var thisMonthLastDate = ....
var 1day = 1000*60*60*24
var date1_ms = recdate
var date2ms =  thisMonthFirstDay.gettime()
if(Math.round(difference_ms/1day) > 7
{var textbox = document,getelementbyid("textbox2");
textbox.value = texbox.value + thisMonthLastDate 

textbox.value = texbox.value + thisMonthFirstDay }

Any examples of how this can be done would be greatly appeciated. Cheers

share|improve this question
add comment

2 Answers

getDate() will give you the day of month (e.g. 18), so if (getDate() <= 7) { outputDate = 1; } If you're having a problem getting the last day of each month for the else statement, I generally use a 12 capacity array with hard-coded values, adding 1 to February if (year % 4 == 0).

share|improve this answer
my problem is that my date that I am passing in is a string and when I try to convert this to a date I get "Nan" (not a number) displayed in JS popup. Is is in the format 30-Jan-2013 –  APW Jan 30 '13 at 21:42
add comment
up vote 0 down vote accepted

I have managed to resolve this after a finding the parseDate() function on a fiddler site. That allowed me to convert the date from this format (31 Jan 2013) to a date and then I could just use the getDay(function) to see if the day was > 7. From there it was easy!

Thanks for above suggestions.

share|improve this answer
add comment

Your Answer


