Get Day name from a date field [RESOLVED]

I have a date field that is entered by the user. Later in the form I would like to have an html field with the date in a Day Name format.
you enter → 3/28/2022
later in the form → Monday

I tried the dow solution in this post = Show day of week in notification [RESOLVED] but it’s not working for me :confused: all I get is the 3/28/2022 date.

Please help!

Hi orl. There is no native capability to carry a value forward to a later spot in the form (reformatted or not.) What method are you using to try and show Monday later in the form, when 3/28/2022 is entered earlier in the form?

Hi Chris, I was using the html field but I am open to other options. It’s really to provide a check before submitting. I keep running into an issue where staff that submit the date don’t know that the day is on a weekend or they have the wrong day of the week.

If you have a multi-page form and the HTML field isn’t on the same page as the Date field, then you should be able to use the merge tag to display the name of the day of the selected date in the HTML field. Here is what you’ll do.

  1. Add a Hidden Field to the form on the same page as the Date field.
  2. Add this Javascript snippet below to your form. The snippet would populate the Hidden field you added above with the Day selected.
var form_id = 123;  // Update '123' to the Form ID
var date_field_id = 4;   // Update '4' to the Date field
var day = 5;  // Update '5' to the ID of the Day Hidden field

$("#input_"+form_id+"_"+date_field_id).on("change",function(){
	var selected_date = new Date( $(this).val());
	var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var weekday = weekdays[selected_date.getDay()];
	
	jQuery("#input_"+form_id+"_"+day).val(weekday).trigger('change');
});

We recommend installing the JS snippet via our free Custom Javascript plugin for Gravity Forms. Once installed, copy and paste the snippet into the Javascript editor and update the IDs.

  1. Insert the merge tag of the Hidden field into the HTML field on the later page to display the selected day of the week in the HTML field.

I hope this helps.

Best,

1 Like

Thanks Samuel, that’s awesome and I’ll give it a shot asap.
Is there a way to add a filter or is that a no go? Thanks again for your solution!

Code I thought would add a custom modifier but it’s not working.

add_filter( ‘gform_merge_tag_filter’, ‘add_day_of_week’, 10, 6 );
function add_day_of_week( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
// if it’s the all_fields merge tag, and field ID2, and the modifier is dow i.e. {all_fields:dow}
if ( $field->type == ‘date’ && $modifier == ‘dow’ ) {
// figure out the day from the date
$timestamp = strtotime( $raw_value );
$value = date( ‘l F j, Y’, $timestamp );
}
// return the unmodified or modified value
return $value;
}

Hi Orl,

We actually have a snippet on this page that allows you to display dates in any format. When you add the snippet/plugin to your website, you can add a modifier to the Date merge tag like so {Date:1:l} and this will display the day of the week for the selected date.

Best,

this is perfect, it works as I was hoping. Thank you again for your quick replies and solutions.

1 Like