JavaScript is one of the most popular Client Scripting language. To make your journey easier in this session we are sharing some functions related to JavaScript Date Functions. Whether you want to add days or subtract days from a specific date or difference between two dates these functions are very useful.
Here I have 5 functions getDateInMilliseconds, getDifferenceInDays, addDays, subtractDays & compareDate. getDateInMilliseconds function returns date in milliseconds & this function is used in addDays & subtractDays functions. getDifferenceInDays function accepts 2 parameters as date 1 & date 2. It returns the difference in days. addDays & subtractDays functions accepts 2 parameters each date & days. The last function compareDate returns true if date 2 is after date 1 else returns false.
Function to get Date In Milliseconds
/* Returns Date object expressed as number of milliseconds since January 1, 1970. This is the internal date format used by JavaScript. */ function getDateInMilliseconds(d) { var str = new String(d); var length = str.length; var month = 0; var day = 0; var year = 0; var dateObj; var ms = 0; if (length == 8) { month = str.substr(0,1); day = str.substr(2,1); year = str.substr(4,4); } else if (length == 9) { var regExp = new RegExp("\\d{2}/\\d{1}/\\d{4}"); var expResult = regExp.test(str); if (expResult) { month = str.substr(0,2); day = str.substr(3,1); year = str.substr(5,4); } else { month = str.substr(0,1); day = str.substr(2,2); year = str.substr(5,4); } } else if (length == 10) { month = str.substr(0,2); day = str.substr(3,2); year = str.substr(6,4); } dateObj = new Date(year, (month - 1), day); ms = dateObj.getTime(); return ms; }
Function to get date difference in Days
/* Returns the number of days between the two Date arguments. Assumes the first argument, d1, is the earlier date. */ function getDifferenceInDays(d1,d2) { var d1ms = getDateInMilliseconds(d1); var d2ms = getDateInMilliseconds(d2); var days = (d1ms - d2ms) / (DAY_IN_MILLISECONDS); return days; }
JavaScript Date Function to add Days
/* Returns a Date string equivalent to the first parameter (date) plus the number of days specified by the second parameter. */ function addDays(date,days) { var dateInMs = getDateInMilliseconds(date); var daysInMs = days * DAY_IN_MILLISECONDS; var newDateInMs = dateInMs + daysInMs; var newDate = new Date(); newDate.setTime(newDateInMs); var year = new String(newDate.getYear()); if (year.length == 2) { year = "19" + yearStr; } var dateString = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + year; return dateString; }
Function to Subtract Days
function subtractDays(date,days) { var dateInMs = getDateInMilliseconds(date); var daysInMs = days * DAY_IN_MILLISECONDS; var newDateInMs = dateInMs - daysInMs; var newDate = new Date(); newDate.setTime(newDateInMs); var year = new String(newDate.getYear()); if (year.length == 2) { year = "19" + year; } // Assemble Date as a string resembling mm/dd/yyyy format var dateString = (newDate.getMonth() + 1) + "/" + newDate.getDate() + "/" + year; return dateString; }
JavaScript Date Function to Compare Dates
/* Returns true if first date parameter, d1, is earlier than second date parameter, d2. Otherwise, returns false. */ function compareDate(d1,d2) { date1 = new Date(d1.value); date2 = new Date(d2.value); if (date1 > date2) { return false; } return true; }