javascript函数日期
我在Drupal中有这个表单,它包含两个日期选择器和两个文本字段。我需要编写一个函数来更新两个文本字段中的值。因此,在textfield first_result中,我需要添加35天,在textfield second_result中,我需要添加3个月,并在两个textfield中设置这些值,在datepicker due_date中,我需要更新此字段以添加一年零45天: 我的代码:javascript函数日期,javascript,drupal,Javascript,Drupal,我在Drupal中有这个表单,它包含两个日期选择器和两个文本字段。我需要编写一个函数来更新两个文本字段中的值。因此,在textfield first_result中,我需要添加35天,在textfield second_result中,我需要添加3个月,并在两个textfield中设置这些值,在datepicker due_date中,我需要更新此字段以添加一年零45天: 我的代码: <?php drupal_add_js(drupal_get_path('module', 'D
<?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;
}
这取决于你所说的月份是什么意思?你说的年是什么意思 如果是1月31日,您添加3个月,则添加3组30天(平均月),或将月值增加3次,使其成为4月31日(不存在) 你说的年份是指增加365天还是2004年2月29日->2005年2月29日(第二个也不存在) 日期操纵有大量的边界情况,所以您确实需要定义您打算做什么。最简单的解决方案是只使用天,然后将其转换为毫秒,然后将其添加到当前日期valueOf()中,然后转换回日期:
function addDays( fromDate, numDays ) {
var millisecondsInDay = 1000 * 60 * 60 * 24;
return new Date( fromDate.valueOf() + (millisecondsInDay * numDays) );
}
如何将表单中的值读取为日期输入。将日期从字符串转换为日期value@sally,我在drupal上不是最新的,但在JS方面,只需使用
date.parse(document.getElementById(dateFieldId))代码>。
function addDays( fromDate, numDays ) {
var millisecondsInDay = 1000 * 60 * 60 * 24;
return new Date( fromDate.valueOf() + (millisecondsInDay * numDays) );
}