Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Thursday, February 21, 2019

Avoid inconsistent Date object definitions

I recently took a pop-quiz question on this, and failed. Errrrr...

Pop Quiz

What is the Date value of the following code?

new Date(2016, 5, 31);

My Answer:

May 31, 2016

Makes sense, right? At least to 70% of us developers who are not JS experts?

See more fun questions at https://app.enkipro.com.

Correct Answer:

June 1, 2016

Since JavaScript early days leveraged a lot techniques and patterns from Java, I looked up both definitions.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Syntax
https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#Date-int-int-int-
https://www.w3schools.com/js/js_dates.asp

My Further Thoughts

With all the new technologies and programming languages (along with updates to these languages), I'm actually shocked that we haven't provide better consistency with a common class instantiation like the Date object. Particularly, in JavaScrpt.

Since the Time parameters all start from 0, this makes sense and is consistent. However, for the Date parameters, since 'day' starts with 1 and makes the most sense, then why not start the month and the year with '1' as well? Nope, instead, the creators decided to make the month slightly difficult (and very difficult when we add in how fast developers need to work nowadays) by starting the value at '0'. But, no one in the world uses '0' to represent a month, and if it's an array index then define the parameter as such for ALL parameters in the Date definition. If you're not already confused, then try the year with a starting value of '1' and see this is actually 1901. Why, just because the creators somehow thought that programming would only be useful for years 1900-1999 (or shortcut values as 0-99). Thanks guys. Especially to Java guys who decided to just deprecate this and recommend using more code for date formatting (i.e. SimpleDateFormat).
*By the way, I wouldn't complain if these two programming languages weren't so currently popular and in demand.

My Solution

To avoid this unnecessarily confusing definition and prevent making mistakes within my apps, I would create a Date wrapper object that would allow for easier reading of my code. And consistency. That's [programming language] agnostic.

Friday, July 22, 2016

JQuery: Setting Default Date on DatePicker

Devs,

I recently was trying to set the default date of a DatePicker that I added to a input field.

form.find('#expires').datepicker( {defaultDate: 7} );

However, the DatePicker would keep displaying the default date value of "1/1/2020". This was driving me nuts as I was looking through the documentation and searching online (i.e. SO) for solutions to my problem to no avail.

http://api.jqueryui.com/datepicker/#option-defaultDate
http://stackoverflow.com/questions/14580749/jquery-datepicker-set-default-date

Root Cause:

After intense thinking and investigation, I finally found my root cause. I already set the predefined value in the field itself which was overriding my jquery default property.

<input type="text" name="expires" class="expires" id="expires" placeholder="Expires" value="1/1/2020" />


Solution #1:

Remove the attribute value in the input field or replace the value with expected default value before passing field to DatePicker.

I chose to replace the value upon displaying of the input field because without the value attribute the input field displays null by default and the DatePicker would open with a default date. I wanted a default date in the input field AND in the DatePicker.

<input type="text" name="expires" class="expires" id="expires" placeholder="Expires" value="1/1/2020" />

form.find('#expires').val( _getDefaultExpirationDate() );
form.find('#expires').datepicker();

/** 
* Get Default Expiration Date
* @return (string) current date + 1 month in format: MM/DD/YYYY
*/
function _getDefaultExpirationDate()
{
var today = new Date();
return tens(today.getUTCMonth()+ 1) +'/'+ tens(today.getUTCDate()) +'/'+ today.getUTCFullYear();
}

/** 
* Tens 
* - converts an integer that's a single digit into double digits
* - useful for dates and times
* @param (int) integer
* @return (string) single digit with leading '0' or double digit
*/
function _tens (integer)
{
while (integer > 99 )
{
integer = integer - 100;
}
return (integer < 10) ? "0"+integer : integer;
}

Easier Solution:

Change the input type from 'text' to 'date' and set default value.
http://www.w3schools.com/jsref/prop_date_value.asp

<input type="date" name="expires" class="expires" id="expires" placeholder="Expires" value="1/31/2016" />

Happy Coding!,
G2