Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 8x 5x | import * as is from '../is/is';
/**
* Returns the current time.
*/
export function now(): number {
return Date.now();
}
/**
* Calculates the time difference in milliseconds between to times.
*
* ```ts
* let startTime = time.now();
*
* window.setTimeout(() {
* let diff = time.timDiffMs(startTime, time.now());
* console.log(diff); // 300
* }, 300);
*
* ```
* @param startTime
* @param endTime
*/
export function timeDiffMs(startTime: number, endTime: number): number {
return endTime - startTime;
}
/**
* Given a startTime and endTime, tests whether the provided testTime
* is within that time.
* @param startTime
* @param endTime
* @param testTime
*/
export function isInBetween(
startTime: Date,
endTime: Date,
testTime: Date
): boolean {
if (startTime && endTime) {
return testTime >= startTime && testTime < endTime;
} else if (startTime) {
return testTime >= startTime;
} else if (endTime) {
return testTime < endTime;
}
return false;
}
/**
* Converts a utc based time that you might created into the local time.
*
* ```
* const myUTCDate = new Date('2020-06-20 03:45:00');
* const theUTCDateInLocalTime = time.utcDateToLocalTimeZone(myUTCDate);
*
*
* // Do Stuff. Now it is in local time zone.
* const diff = thisUTCDateInLocalTime - new Date();
* ```
* Now the theUTCDateInLocalTime is the UTC time adjusted to your
* local computer timezone.
*
* @param utcDate
*/
export function utcDateToLocalTimeZone(utcDate: Date): Date {
// Safari for some reason, defaults to utc and does the conversion
// for you.
if (is.safari()) {
return utcDate;
} else {
const timeOffsetInMS: number = utcDate.getTimezoneOffset() * 60000;
return new Date(utcDate.getTime() - timeOffsetInMS);
}
}
/**
* Create a local timezone date string. This is mainly here to normalize
* browser differences. Safari creates a new date in utc whereas
* other browsers create the date based on the users timezone.
*
* By using this method, you are guaranteed that the created date is adjusted
* to the current users computer time.
* ---
*
* const myDate = time.createLocalTimeZoneDate('2021-06-20T03:45:00');
* const myDate2 = time.createLocalTimeZoneDate('2021-06-20 03:45:00');
*
* ---
*
* The localTimeDate would be the localtime on the users computer.
*
* ---
* const myDate = time.createLocalTimeZoneDate('2021-06-20T03:45:00');
* ---
*/
export function createLocalTimeZoneDate(localTimeDate: string): Date {
if (is.safari()) {
const utcDate = new Date(localTimeDate);
const timeOffsetInMS: number = utcDate.getTimezoneOffset() * 60000;
// Add it since safari creates it in UTC.
return new Date(utcDate.getTime() + timeOffsetInMS);
} else {
return new Date(localTimeDate);
}
}
/**
* Converts a pacific time over to utc.
*
* ---
* const myPacificTime = '2020-12-12 03:34:00';
* time.pacificTimeToLocalTimeDate(myPacificTime);
*
* ---
*
*/
export function pacificTimeToLocalTimeDate(pacificTime: string): Date {
// First create the time locally.
// 7 Hour diff.
const pacificOffset: number = 420 * 60000;
// UTC
const utc = new Date(new Date(pacificTime).getTime() + pacificOffset);
return utcDateToLocalTimeZone(utc);
}
/**
* Converts a pacific time over to utc.
*
* ---
* const myEastTime = '2020-12-12 03:34:00';
* time.easternTimeToLocalTimeDate(myEastern);
*
* ---
*
*/
export function easternTimeToLocalTimeDate(pacificTime: string): Date {
// First create the time locally.
// 4 Hour diff.
const pacificOffset: number = 240 * 60000;
// UTC
const utc = new Date(new Date(pacificTime).getTime() + pacificOffset);
return utcDateToLocalTimeZone(utc);
}
/**
* Use to calculate the countdown to a given time. Note that the
* currentTime and endTime should both be in the same local timezone.
*
* Returns an object with days, hours, minutes, seconds.
*
* ```
* const now = new Date();
* const endTime = new Date('2021-06-20 03:34:00 UTC');
* const countDown = time.countdown(now, endTime);
*
*
* countdown.days; // the number of days
* countdown.hours; // the number of hours
* countdown.minutes; // the number of minutes
* countdown.seconds; // the number of seconds
* ```
*/
export function countdown(currentTime: Date, endTime: Date): Object {
const time = endTime.getTime() - currentTime.getTime();
return {
days: Math.floor(time / (1000 * 60 * 60 * 24)),
hours: Math.floor((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
minutes: Math.floor((time % (1000 * 60 * 60)) / (1000 * 60)),
seconds: Math.floor((time % (1000 * 60)) / 1000),
};
}
|