开发者

javascript function date

I have this form in Drupal that contains two datepickers and two textfields. I need to write a function that updates the values in the two textfields. So in the textfield first_result I need to add 35 days and in the textfield second_result i need to add 3 months and to set these values in the two textfields, and in the datepicker due_date I need to update this field to add one year and 45 days:

My code:

  <?php

  drupal_add_js(drupal_get_path('module', 'Date') .'/script.js');


 function Pregnancy_menu() {


   $items['form/Date'] = array(
   'title' => 'Date Calculator',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('Date_form'),
    'access callback' => TRUE,
    );
    return $items;
     }


   function Date_form($form_type) {
   drupal_add_js(drupal_get_path('module', 'Date') .'/script.js');


    $form['First_Period'] = array(
    '#type' => 'date_popup',
    '#title' => 'Date of your last menstrual period',
    '#date_format' => 'Y-m-d',
    '#date_text_parts' => array('year'),
    '#date_increment' => 30,
    '#date_year_range' => '-1:0',
    '#default_value' => date(Y) . date(M) . date(D),
     );



     $form['Calculate_Forward'] = array(
     '#type' => 'button',
     '#value' => t('Calculate Forward'),
     '#attributes' => array('onclick' => "testing()"),
      );


       $form['Reset_form'] = array(
       '#name' => 'clear',
    '#type' => 'button',
    '#value' => t('Reset this Form'),
    '#attributes' => array('onclick' => 'this.form.reset(); return false;'),
      ); 


       $form['First_result'] = array(
      '#type' => 'textfield',
      '#title' => t('first result'),     
      '#prefix' => '<div style="float:left;">',
      '#suffix' => '</div>',
 '#default_value' => 'Hello',


     ); 

     $form['Second_result'] = array(
    '#type' => 'textfield',
    '#title' => t('second result'),     
    '#prefix' => '<div style="float:left;">',
    '#suffix' => '</div>',
    );

     $form['Due_Date'] = array(
     '#type' => 'date_popup',
     '#title' => 'Estimated Due Date',
     '#date_format' => 'Y-m-d',
     '#date_text_parts' => array('year'),
      '#date_increment' => 30,开发者_运维百科
     '#date_year_range' => '+1:+1',
     '#default_value' => date(Y) . date(M) . date(D),
   );




  return $form;
  }

//the javascript function script.js

var testing()=function()
 {
 //how to do the calculation here

 } 


This depends on what you mean by month? and what you mean by year?

If it is Jan 31 and you add 3 months is that adding 3 sets of 30 days (avg month), or incrementing the month value 3 times so it becomes Apr 31 (which doesn't exist).

And by year do you mean add 365 days or Feb 29 2004 -> Feb 29 2005 (again the second doesn't exist).

Date manipulation has a ridiculous number of boundary cases, so you really need to define what you are intending to do. The simplest solution would be to only use days and then convert that to milliseconds then add it to your current dates valueOf() then convert back to date:

function addDays( fromDate, numDays ) {
    var millisecondsInDay = 1000 * 60 * 60 * 24;
    return new Date( fromDate.valueOf() + (millisecondsInDay * numDays) );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜