I need to set a value for a date field with type datetime in hook_form_alter
. I tried with below codes. But it is not working.
$form["field_event_date"]["und"][0]['#default_value']['value'] = $new_date;
$form_state['values']['field_event_date']['und'][0]['value']['date'] = $new_date;
$form['field_event_date'][LANGUAGE_NONE][0]['#default_value']['value'] = $new_date;
This $new_date
will be a value from another field in this form and it should be in '2014-09-23 00:00:00' format. And 'field_event_date' will be a hidden field.
I can save the value to this field using the node_presave
function. Please see the below code. If anyone trying to save a value to a date field with a static value, you can use this method.
function MY_MODULE_node_presave($node) {
$node->field_event_date['und'][0]['value'] = '2014-09-23 00:00:00';
}
But in my case, I need to get the value from a field that defined in the form alter. And need to set this value to the field_event_date. Can anyone help me on this? Is there any way to get a custom field value in node_presave
function?