Ashish Singh

Web Hacking. Typify.

JavaScript Date Difference Using Format Yyyy-mm-dd

| Comments

Finding date differences in JavaScript Date objects is trivial but it takes a turn when dealing with multiple timzones. Well, that is up for another discussion but for now let’s consider 2 cases of finding difference between two dates using below date formats.

Using date format - mm/dd/yyyy & yyyy-mm-dd to find difference

Difference between using mm/dd/yyyy & yyyy-mm-dd
1
2
3
4
5
6
7
8
9
10
11
var start_date = new Date('12/01/2013'),
    end_date   = new Date(),
    one_day_in_milliseconds = 1000*60*60*24,
    date_diff = Math.floor( ( end_date.getTime() - start_date.getTime() )/one_day_in_milliseconds ) ;

    console.log("Result 1 - Date difference between ",end_date, " & ",start_date, " - ", date_diff);

var s_date = new Date('2013-12-01'),
    date_diff_2 = Math.floor( ( end_date.getTime() - s_date.getTime() )/one_day_in_milliseconds ) ;

    console.log("Result 2 - Date difference between ",end_date, " & ",s_date, " - ", date_diff_2);

You may notice that the difference from both these ways differs by 1. If this is the case and you’re using second format [yyyy-mm-dd], then you can solve this using following technique.

Solution

Difference between 2 dates using yyyy-mm-dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function getNewDate(dateString){

  var date = dateString ? new Date(dateString) : new Date(),
      timezoneOffset = date.getTimezoneOffset();

      return new Date( date.getTime() + timezoneOffset*60*1000*(dateString?1:0) );
}

var start_date = getNewDate('2013-12-01'),
    end_date   = getNewDate(),
    one_day_in_milliseconds = 1000*60*60*24,
    date_diff = Math.floor( ( end_date.getTime() - start_date.getTime() )/one_day_in_milliseconds ) ;

    console.log("Date difference between ",end_date, " & ",start_date, " - ", date_diff);

Here, we are considering the timezone offset when a new Date object is created using dateString in format yyyy-mm-dd.

Deploy to Amazon EC2 Using GIT

| Comments

Git is an amazing and indispensable tool. We use it for tracking all of our development work. It just becomes more cooler when you are able to push your code to a production or sandbox server with a simple git push.

Below listed are simple yet powerful steps to get you pushing your code to an Amazon EC2 server or any other linux server, where you have SSH access.

Install git on your Server

First SSH into your remote server from you local machine. Use the private key file (.pem key) that you’ve downloaded from the Amazon EC2 server setup process on aws console. If you don’t have it yet, Go to your aws console -> EC2 server -> Network & Security -> Key Pairs -> Create Key Pair

Once you’ve created the private key, it’ll automatically be downloaded to your machine. Move it a folder where you wish to keep the key. Also, change its permission to 0400

1
chmod 0400 /path/to/my-amazon-key.pem

Now, lets use this private key to connect to your newly created ec2 server.

Open console and type:

1
ssh -i /path/to/my-amazon-key.pem ec2-user@example-webserver.amazonaws.com

If you’ve downloaded and set key’s permission correctly, you should now be on the ec2 server’s console. Now, install git on the ec2 server.

1
2
// on your ec2 server
ubuntu@ip-xxx-xxx-xx-xxx:~$ sudo apt-get install git

Perfect! You’re one step closer to push your local git repo to the ec2 server.

Setup SSH Access for different ec2 servers

Say, you’ve more than one ec2 servers. To make your life easier, it is desirable to set up a SSH config file which basically holds information needed to SSH into your remote ec2 server.

On your local machine, open file [or create one] ~/.ssh/config and add this code for each of your ec2 servers. Replace Host, Hostname, User and IdentityFile values with yours.

1
2
3
4
5
6
7
8
9
Host my-prod-server
Hostname example-webserver.amazonaws.com
User ec2-user
IdentityFile /path/to/my-amazon-key.pem

Host my-sandbox-server
Hostname example-webserver.amazonaws.com
User ec2-user
IdentityFile /path/to/my-sandbox-amazon-key.pem

Test the connection

1
ssh my-prod-server

Hurray, if you get connected to your server otherwise debug using this.

1
ssh -Tvvv my-prod-server

When Options -Indexes in .htaccess Doesn’t Work

| Comments

We’ve our sandbox server running on Amazon EC2. There are a lot of projects hosted there with each project’s .htaccess taking care of it. Apart from the various tasks that the .htaccess does, it restricts directory browsing using simple directive

1
2
3
<IfModule mod_autoindex.c>
  Options -Indexes
</IfModule>

But somehow on the root folder of the sandbox, this directive didn’t work. This implies, a directory specific directive was somewhere being issues. These are a few places to look for

1
2
3
/etc/apache2/httpd.conf
/etc/apache2/apache.conf
/etc/apache2/sites-enabled/000-default

In our case, the below directives were present in the /etc/apache2/sites-enabled/000-default file.

1
2
3
4
5
6
7
8
9
10
11
DocumentRoot /var/www
<Directory />
        Options FollowSymLinks
        AllowOverride None
</Directory>
<Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews Includes
        AllowOverride None
        Order allow,deny
        allow from all
</Directory>

This was changed to

1
2
3
4
5
6
7
8
9
10
11
12
13
DocumentRoot /var/www
<Directory />
        #Options FollowSymLinks
        Options -Indexes
        #AllowOverride None
</Directory>
<Directory /var/www/>
        #Options Indexes FollowSymLinks MultiViews Includes
        #AllowOverride None
        #Order allow,deny
        #allow from all
        Options -Indexes
</Directory>

and it worked!

Setup SMTP on Bugzilla

| Comments

Our startup just celebrated its 1st anniversary and we marked this by making bugzilla our primary bug tracker. The bugzilla docs will help you setting it up.

We are hosting it on Ubuntu 12.04 LTS 64bit Amazon EC2 micro instance and using Amazon RDS mysql as backend. For sending bugzilla emails alerts, we choose SMTP.

We followed this guide but ran into some problems on the first step of installing perl module Email::Send::SMTP::TLS. The error we got was

Building and testing Net-SSLeay-1.52 … FAIL

This probably means that your Ubuntu is missing the Perl module for Secure Sockets Layer (SSL). You install it like this.

1
sudo apt-get install libnet-ssleay-perl

Next, we go ahead with the installation of Email::Send::SMTP::TLS module.

1
2
sudo apt-get install cpanminus
sudo cpanm Email::Send::SMTP::TLS

Now, continue following this guide

Hello World - Chao Procrastination

| Comments

Hello all.

It has been quite sometime since I entered the amazing world of Web development and the beauty of Open Source. I intend to post things here, that will hopefully help some people in some ways. This is going to be an avenue for sharing stuff that I create or think are cool in my areas of interest. A lot of stuff here is going to be highly opinionated.