Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/jwblogger/jwBlogger/wp-includes/functions.php on line 6121

Notice: Function wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. This notice was triggered by the codepen-embed-script handle. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.) in /home/jwblogger/jwBlogger/wp-includes/functions.php on line 6121
kendo ui – Jeff Wilkerson's Blog https://blog.jeffwilkerson.net The yoga of web development Thu, 20 Apr 2017 21:41:42 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.5 Unbirthdays! Playing with Kendo UI DatePicker https://blog.jeffwilkerson.net/2017/04/20/unbirthdays-playing-with-kendo-ui-datepicker/ https://blog.jeffwilkerson.net/2017/04/20/unbirthdays-playing-with-kendo-ui-datepicker/#respond Thu, 20 Apr 2017 21:41:42 +0000 http://blog.jeffwilkerson.net/?p=316 I’ve been playing with a massive Kendo UI Grid for the past few months. I really enjoy Kendo UI – I can build functionality quickly, and the forums are very active and helpful. But there is so much grid I can take!!

I decided to take a break from my grid for a few days and familiarize myself with the Kendo UI Datepicker. I gave myself a project to calculate the number of unbirthdays someone has had. So many people tell their age in the number of birthdays. Well, wouldn’t it be crazy if we all spoke our age in terms of unbirthdays? (These all the thoughts one has when they write lots of javascript and suffer from a lack of sunlight.)

Plus, today is the 34th anniversary of my first unbirthday!

See the Unbirthday Calculator at the bottom of this blog post.

Quick Takeaways

Despite the absurdity of the purpose of this project, these are my takeaways from this exercise:

  • Initialize a Kendo UI DatePicker
  • Apply a validator to a form field
  • A renewed, deepened understanding of the Date Object

The Code

Check out the code on Github.

Calculating Unbirthdays

To calculate unbirthdays, you first calculate the number of days in between today and your birth date, then subtract the number of birthdays. Here is what that looks like in javascript:

var calcUnBirthday = function(bday, today) {

	//get difference between two dates, in milliseconds
	var diff = today.getTime() - bday.getTime();

	//find age by converting difference into Date, then subtracting from 1970.
	//ref: http://stackoverflow.com/questions/4060004/calculate-age-in-javascript
	var diffDate = new Date(diff);
	var age = Math.abs(diffDate.getUTCFullYear() - 1970);
	
	//calculate days since birthdate by converting the difference into days.
	var daysSince = Math.ceil(diff / (1000 * 60 * 60 * 24));

	//calculate unbirthdays
	var unBirthdays = daysSince - age;

	return unBirthdays;
};

Working with the Kendo UI DatePicker

As with all the Kendo UI Widgets, there is ample examples and documentation provided by the Kendo UI Team.

One thing that stumped me is how to allow the user to type in a date, validate the input, then trigger the change event of the DatePicker. I was confused as to why I couldn’t trigger the change event of the DatePicker by typing in a value. This led me to implement a Validator, as well as a ‘keyup’ event handler for when the user presses ‘Enter’ and submits their input. By wrapping my DatePicker in a Validator, and adding a ‘keyup’ event handler, I am able to allow the user to type in a value if they choose to.

Take a look at all the code to see for yourself what I’m talking about!

The Result

Without further ado, here is the unbirthday calculator.

Special Thanks to…

@Event_Hoorizon for creating this simple codepen showing how to create confetti.

]]>
https://blog.jeffwilkerson.net/2017/04/20/unbirthdays-playing-with-kendo-ui-datepicker/feed/ 0