Category Archives: Programming

Find the month difference between two different dates in Javascript

Here is the approach:

  1. Subtract the year values from both the dates and multiply the result by 12.
  2. Subtract the month values from both the dates
  3. Subtract the day values from both the dates and divide the result by 30
  4. Add all the above values, the integer part of it as the answer.


var startdate = new Date('2011-05-01');
var enddate = new Date('2021-04-01');
var MonthDifference =
//Difference in years, multiplied by 12.
((enddate.getFullYear() - startdate.getFullYear()) * 12) +
//Difference in months.
(enddate.getMonth() - startdate.getMonth()) +
//integer value of( Difference in days, divided by 30.
Math.floor((enddate.getDate() - startdate.getDate()) / 30);

1. Math.floor() function is used to round a number downward to its nearest integer value.
2. The above method does not return the exact month difference. It only returns the completed months value.
3. Online tool for calculating the same can be found at here.


Generate insert scripts from a result set of SQLserver table

We have a table called employee which contains two columns eno, esal. The table contains ten records.
We are supposed to create a similar table with the same values in other server/database. We can create the same table by using the same CREATE table definition. Using the below method we can generate insert scripts which we can run in the destination, to add data rows. This method uses the concept of cursors.
Employee table has the following definition:

CREATE TABLE [dbo].[employee](
[eno] [int] NULL,
[esal] [int] NULL

Both the employee number, and salary are integers.
Below steps need to be executed in the source server/Database:

  1. Click on Results to File icon in the SQL Editor menu in Management Studio. This can be used to generate all the insert scripts in a text file. Shortcut to do this is (Ctrl+ Shift+ F).
  2. Run the below cursor query:
DECLARE @eno INT,@esal INT
SELECT eno,esal FROM empsalary
OPEN insertquery
FETCH NEXT FROM insertquery INTO @eno,@esal
PRINT 'insert INTO empsalary values('+CONVERT(VARCHAR,@eno)+','+CONVERT(VARCHAR,@esal)+')'
FETCH NEXT FROM insertquery INTO @eno,@esal
CLOSE insertquery
DEALLOCATE insertquery
  1. Running the above query will open file explorer. Select all files and save the file as insert query.txt.
  2. Use that file to add records in destination server.