Skip to content

Instantly share code, notes, and snippets.

@intoxopox
Last active October 12, 2018 17:14
Show Gist options
  • Save intoxopox/020bd5323116b5a91b07fd9c1ee1af0a to your computer and use it in GitHub Desktop.
Save intoxopox/020bd5323116b5a91b07fd9c1ee1af0a to your computer and use it in GitHub Desktop.
DateUtil - Handy Date functions
////////////////////////////////////////////////////////////////////////////////
// Copyright(C) 2018 David Hamiter
////////////////////////////////////////////////////////////////////////////////
'use strict';
abstract class DateUtil {
//----------------------------------------------------------------------
//
// Properties
//
//----------------------------------------------------------------------
static DEFAULT_FORMAT: string = "MM/DD/YYYY";
//----------------------------------------------------------------------
//
// Constructor
//
//----------------------------------------------------------------------
private constructor() { } // Static class cannot be instantiated
//----------------------------------------------------------------------
//
// Methods
//
//----------------------------------------------------------------------
//only supports format MM/DD/YYYY
static format(formatted: string): Date {
if (formatted.charAt( 1) === "/")
formatted = "0" + formatted;
if (formatted.charAt(4) === "/")
formatted = formatted.substring(0, 3) + "0" + formatted.substring(3);
const month: number = parseInt(formatted.substring(0, 2)) - 1;
const day: number = parseInt(formatted.substring(3, 5));
const year: number = parseInt(formatted.substring(6, 10));
return new Date(year, month, day);
}
//only supports format MM/DD/YYYY HH:MM:SS
static formatFullDate(formatted: string): Date {
if (formatted.charAt(1) === "/")
formatted = "0" + formatted;
if (formatted.charAt(4) === "/")
formatted = formatted.substring(0, 3) + "0" + formatted.substring(3);
const month: number = parseInt(formatted.substring(0, 2)) - 1;
const day: number = parseInt(formatted.substring(3, 5));
const year: number = parseInt(formatted.substring(6, 10));
const hour: number = parseInt(formatted.substring(11, 13));
const minute: number = parseInt(formatted.substring(14, 16));
const second: number = parseInt(formatted.substring(17, 19));
return new Date(year, month, day, hour, minute, second);
}
static getYearsPassed(oldDate: Date): number {
return DateUtil.dateDiff("y", new Date(), oldDate);
}
static getMonthsPassed(oldDate: Date): number {
return DateUtil.dateDiff("m", new Date(), oldDate);
}
static getDaysPassed(oldDate: Date): number {
return DateUtil.dateDiff("d", new Date(), oldDate);
}
static getAgeString(formattedDob: string): string {
const date: Date = DateUtil.format(formattedDob);
var age: string;
var ageResult: number;
if ((ageResult = DateUtil.getYearsPassed(date)) >= 1) {
age = String(ageResult) + " Years";
} else if ((ageResult = DateUtil.getMonthsPassed(date)) >= 1) {
age = String(ageResult) + " Months";
} else if ((ageResult = DateUtil.getDaysPassed(date)) >= 1) {
age = String(ageResult) + " Days";
}
return "Age " + age;
}
/*static quickFormat(date: Date, dateFormat: string): string {
var f: DateFormatter = new DateFormatter();
f.formatString = dateFormat;
return f.format(date);
}*/
/**
* Computes the difference between two dates in a specified type. i.e. seconds, years etc.
* @param datePart a return type. Valid inputs are: "s", "n", "h", "d", "m", "y".
* @param date1 start date.
* @param date2 end date.
* @return the difference between two dates.
*/
static dateDiff(datePart: "s"| "n"| "h"| "d"| "m"| "y", date1: Date, date2: Date): number {
return DateUtil.getDatePartHashMap()[datePart.toLowerCase()](date1, date2);
}
static clearTime(date: Date): Date {
return new Date(0);
}
static copyDate(date: Date): Date {
return new Date(date.getTime());
}
static setTime(date: Date, time: number): Date {
date.setHours(Math.floor((time / (1000 * 60 * 60)) % 24) );
date.setMinutes(Math.floor((time / (1000 * 60)) % 60) );
date.setSeconds(Math.floor((time / 1000) % 60) );
date.setMilliseconds( Math.floor(time % 1000) );
return date;
}
static addTime(date: Date, time: number): Date {
date.setMilliseconds(date.getMilliseconds() + time);
return date;
}
private static getDatePartHashMap(): { [index: string]: Function } {
const dpHashMap: { [index: string]: Function } = Object.create(null); // use Object.create(null) instead of {} so object prototype keys not included
dpHashMap["s"] = DateUtil.getSeconds;
dpHashMap["n"] = DateUtil.getMinutes;
dpHashMap["h"] = DateUtil.getHours;
dpHashMap["d"] = DateUtil.getDays;
dpHashMap["m"] = DateUtil.getMonths;
dpHashMap["y"] = DateUtil.getYears;
return dpHashMap;
}
static compareDates(date1: Date, date2: Date): number {
return date1.getTime() - date2.getTime();
}
static compareDatesNoTime(date1: Date, date2: Date): number {
return DateUtil.clearTime(DateUtil.copyDate(date1)).getTime() - DateUtil.clearTime(DateUtil.copyDate(date2)).getTime();
}
private static getSeconds(date1: Date, date2: Date): number {
return Math.round(DateUtil.compareDates(date1, date2) / 1000);
}
private static getMinutes(date1: Date, date2: Date): number {
return Math.round(DateUtil.getSeconds(date1, date2) / 60);
}
private static getHours(date1: Date, date2: Date): number {
return Math.round(DateUtil.getMinutes(date1, date2) / 60);
}
private static getDays(date1: Date, date2: Date): number {
return Math.floor(DateUtil.getHours(date1, date2) / 24);
}
private static getMonths(date1: Date, date2: Date): number {
const yearDiff: number = DateUtil.getYears(date1, date2);
var monthDiff: number = date1.getMonth() - date2.getMonth();
if (monthDiff < 0) {
monthDiff += 12;
}
if (date1.getDate() < date2.getDate()) {
monthDiff -= 1;
}
return 12 * yearDiff + monthDiff;
}
private static getYears(date1: Date, date2: Date): number {
return Math.round(DateUtil.getDays(date1, date2) / 365);
}
static getDaysInMonth(month: number, year: number): number {
const m: number[] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (month != 2) return m[month - 1];
if (year % 4 != 0) return m[1];
if (year % 100 == 0 && year % 400 != 0) return m[1];
return m[1] + 1;
}
static addMonths(date: Date, months: number): Date {
if (months === 0)
return DateUtil.copyDate(date);
var currentMonth: number = date.getMonth();
var newMonth: number;
var currentYear: number;
var diffYears: number;
currentYear = date.getFullYear();
if (months < 0) {
newMonth = currentMonth + months;
diffYears = Math.abs(Math.floor(newMonth / 12));
currentYear -= diffYears;
if (newMonth < 0)
currentMonth = 12 - Math.abs((newMonth % 12));
else if (newMonth === 12 || newMonth === 0)
currentMonth = 12;
else
currentMonth = (newMonth % 12);
} else {
newMonth = currentMonth + months;
diffYears = Math.abs(Math.floor(newMonth / 12));
currentYear += diffYears;
currentMonth = (newMonth % 12);
}
return new Date(currentYear, currentMonth, date.getDate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment