Dec 30, 2015

How to show a friendly time difference in javascript

This is a short reusable coding snippet that could save you some programming time. I was creating one for myself, and later figured out I would put it up here on my blog as well, in case someone needs it.

The objective is to take in a date as input and then provide the date difference in a meaningful way. For example, if the difference is few seconds, it should say "A moment ago", instead of 17.563 seconds. Also it makes sense to say "Yesterday", instead of "27.75 hours ago". The script below is created  using such assumption.

//t is a standard ISO date string like '2015-12-29T19:07:41Z'

function getFriendlyTime(t)
{
 var diff = Date.now() - Date.parse(t); 
 var seconds = 1000, minutes = 1000 * 60, hours = 1000 * 60 * 60, days = 1000 * 60 * 60 * 24, weeks = 1000 * 60 * 60 * 24 * 7, months = 1000 * 60 * 60 * 24 * 30, year = 1000 * 60 * 60 * 24 * 365; 
 if(diff < 2 * minutes) return "A moment ago";
 if(diff < hours) return Math.floor(diff/minutes) + " mins ago";
 if(diff < days) return (Math.floor(diff/hours)==1)?"an hour ago":Math.floor(diff/hours) + " hrs ago";
 if(diff < weeks) return (Math.floor(diff/days)==1)?"yesterday":Math.floor(diff/days) + " days ago";
 if(diff < months) return (Math.floor(diff/weeks)==1)?"last week":Math.floor(diff/weeks) + " weeks ago";
 if(diff < year) return (Math.floor(diff/months)==1)?"last month":Math.floor(diff/months) + " months ago";
 return (Math.floor(diff/year)==1)?"an year ago":Math.floor(diff/year) + " yrs ago";
}

Sep 12, 2015

End of the American Dream

Greatness in any sport is all about unimaginable outcomes. And when it comes to Tennis, an unseeded player defeating a 21-time grand slam champion in a major semi-final game is convincing enough to call it one. Here is an Italian tennis player Roberta Vinci (32) who has been coming to the U.S Open every year since 2001, and reaches a career best - Round of 4. On the other side, we have a 21-time Grand Slam Champion, the phenomenal Serena Williams, who is marching her way towards a historic calendar year (which means winning the 4 grand slam tournaments - French Open, Wimbledon,  Australian Open and U.S Open all in the same year). The odds of Serena losing this game was 1:30 before the start, noted major betting websites. This meant that anyone betting $100 on Vinci to win would get back $3000 on a successful outcome. 


What was expected to be a dull, boring semi-final turned out later as one of the emphatic wins in grand-slam history. Vinci was a set behind, before regaining the courage to come back and win the 2nd and 3rd sets. The moment ended the American's dream, and left the American fans heart-broken. "Sorry Guys, Sorry to Serena and the American People, Today is MY Day", said Vinci during her post match interview.

The finals will feature Roberta Vinci against Flavia Pennetta, both Italians, making it a first-time ever All-Italian U.S Open Final.

Update: Vinci lost to Pennetta in the finals in straight sets, but she was more than happy with being the finalist, grabbing a $1.6M pay check. To everyone's shock, Pennetta declared it will be her last and final U.S Open, and will be retiring with this edition.   

Jan 31, 2015

Lookup / Describe a Stored Procedure in SQL Server

I was working with a invisible database for which I had a JNDI connection to but not its credentials to get onto a SQL Server Management Studio IDE and look up the objects such as Tables and Stored procedures. So I end up working with a Stored Proc which slammed me repeatedly for several hours with a "Too many arguments" error, was trying to find out any possible ways to lookup the signature of the SP.

For SQL tables, we had queries like "DESC TABLE_NAME" to lookup the columns, and I wanted something similar to lookup the input/output definition of a stored procedure. So after going through the documentation of SQL server, I find this command that got me exactly what I need. sp_helptext is the built in function that helped me see the source code, and finally figure out how it works. This is the syntax.

sp_helptext @objname = N'dbo.spNameGoesHere'";

Ha! I would no longer worry when that DBA goes on vacationing!

You may also like these writeups

Related Posts with Thumbnails