The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Get current date of year?

Labels

This Discussion is public

Notifications

Hello,

Does anyone know how I could have one field with a date, and another field to show how many days in the year for that date?

Example: 10/1/17 = 274

Thank you!

Using the PHP Date format feature of the shortcode "date", you would do this -> [date format="z"]. "z" is the day of the year in PHP Date.

Thanks Bobby!

Is that something that could immediately produce a result when I enter the initial date (i.e. as opposed to after submitting the form)?

I don't know how to enter that into the form's functions. Would it be something along the lines of this snippet?

https://formidableforms.com/knowledgebase/frm_validate_field_entry/#kb-calculate-final-date

PHP is a server-side scripting language, which means to see the results of the date calculation immediately in a user's browser, you would have to make an Ajax call to calculate the day on the server and send the results back to browser.

Immediate calculation results are best handled through jQuery. jQuery executes in the client's browser. No round trip to the server required. If you Google "jquery day of year", you'll get millions of results returned.

Hey I got it. Code below.

Is there a way so that this is pre-populated when the form loads? As it stands it only populates when I edit the field.

Thanks!

<script type="text/javascript">

jQuery(document).ready(function($) {
$('#field_dayofyear').change(function() {
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);

$("#field_dayofyear").val(day);
$("#field_dayofyear").change();

});
});
</script>

 

 

What you've listed is strictly a "change" function as encapsulated by:

$('#field_dayofyear').change(function() {

Maybe try this:
jQuery(document).ready(function(){
$('#field_dayofyear').bind('change', function () {
...
});
$('#field_dayofyear').trigger('change');
});

Discussion closed.