Calculated Date Issue

Hi,

I’m developing a gallery management system. The user won’t know the exact content date for most of the records but they will know the approximate month & year.

For this reason I created 3 text fields (date_day, date_month & date_year). Each field is attached with drop down value lists.

I created a calcultation which calculates based on the user input. If the user just input month & year the calc adds the first day of the month and make it a proper date.. and so on

var dateStr = null

if ( date_day != "" && date_month !="" && date_year != ""){
dateStr = new Date( parseInt(date_year), parseInt(date_month)-1, parseInt(date_day))
} 
else if ( date_day == "" && date_month =="" && date_year == ""){
dateStr = null
} 
else if ( date_day != "" && date_month != ""){
 dateStr = new Date(1901, parseInt(date_month)-1, parseInt(date_day))
}
else if ( date_day != "" && date_year != ""){
 dateStr = new Date(parseInt(date_year), 0, parseInt(date_day))
}
else if ( date_month != "" && date_year != ""){
 dateStr = new Date(parseInt(date_year), parseInt(date_month)-1 , 01)
}
else if ( date_day != "" ){
 dateStr = new Date(1901, 0 , parseInt("date_day"))
}
else if ( date_month != "" ){
 dateStr = new Date(1901, parseInt("date_month")-1 , 01)
}
else if ( date_year != "" ){
 dateStr = new Date(parseInt("date_year"), 0 , 01)
}

return dateStr

This is not working well. The issues I’m facing are:

  • When selecting all the 3 fields the calculation works well. This is fine

  • When selecting no fields according to the calc there should not be any values but I see “01-01-1970”

Let’s say all the 3 fields are empty

  • When selecting just the day or just the month the calculation does not work. It still shows “01-01-1970”

  • The same happens when selecting month & day. It still shows “01-01-1970”

  • As I said before when I select the 3 fields then it works as expected. But the strange thing is after I selected the 3 fields and if I select day or month then the calculation works..

I’m sure I’m doing something worng

Help needed…

Thanks

Ahmad, dont’ know if this will answer your question but:

  1. I see your fields are strings. Why? But still I would check them with ’ if (!date_day && !date_month && !date_year) '. This way you check for “”, 0 or NULL.
  2. I would set the fields to NULL when you open the form. This way they are really empty…