Skip to content

Latest commit

 

History

History
53 lines (46 loc) · 1.39 KB

File metadata and controls

53 lines (46 loc) · 1.39 KB

javascript-dates

This repository contains all the functions to manage and manipulate javascript dates, such as getting the time in am and pm format or getting the fullYear. Hope these functions will help and save time.

Check if date is valid

function isValidDate(value) {
    var dateWrapper = new Date(value);
    return !isNaN(dateWrapper.getDate());
}

Compare Number of Days between two dates

function compareNumberOfDays(startDate,endDate) {
		return result = startDate.getDay() - endDate.getDay();
}

Get Full Month Name

function getMonthName(today) {
		var monthNames = ["January", "February", "March", "April", "May", "June",
				  "July", "August", "September", "October", "November", "December"
				];
    return monthNames[today.getMonth()]
}

Compare Time Between two dates

function compareTimeBetweenTwoDates(date1, date2) {
	var timediff = (date1.getTime() - date2.getTime()) / 1000; // time in seconds
  return timediff / 60 // time in minutes
}

Time in AM/PM Format

function formatAMPM(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;
  return strTime;
}