more time utils
This commit is contained in:
parent
49a2df8cd4
commit
fa66820c76
10
index.html
10
index.html
@ -6,14 +6,10 @@
|
||||
</head>
|
||||
<body>
|
||||
<script type="module">
|
||||
import {fromCsv, toCsv} from './dist/index.mjs';
|
||||
import {adjustTz, formatDate} from './dist/index.mjs';
|
||||
|
||||
const csv = '_id,any,boolean,date,file,foreignKey,formula,javaScript,number,string,_createdBy,_createdDate,_updatedBy,_updatedDate\n' +
|
||||
'34,,true,,,,,,,,system,2024-11-09T12:06:50.023Z,system,2024-11-09T17:44:42.512Z\n' +
|
||||
'37,,,,,,,,,,system,2024-11-09T18:53:21.793Z,system,2024-11-09T18:53:21.793Z\n' +
|
||||
'38,,,,,,,,,,system,2024-11-09T18:53:21.796Z,system,2024-11-09T18:53:21.796Z';
|
||||
|
||||
console.log(fromCsv(csv));
|
||||
console.log(formatDate(new Date(), 'HH:mm:ss z'));
|
||||
console.log(formatDate(adjustTz(new Date(), 0), 'HH:mm:ss z', 'UTC'));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.22.7",
|
||||
"version": "0.22.8",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
38
src/time.ts
38
src/time.ts
@ -1,5 +1,3 @@
|
||||
import {strSplice} from './string.ts';
|
||||
|
||||
/**
|
||||
* Like setInterval but will adjust the timeout value to account for runtime
|
||||
* @param {Function} cb Callback function that will be ran
|
||||
@ -22,14 +20,21 @@ export function adjustedInterval(cb: Function, ms: number) {
|
||||
}
|
||||
}
|
||||
|
||||
export function adjustTz(date: Date, offset: number) {
|
||||
const currentOffset = date.getTimezoneOffset();
|
||||
offset = currentOffset - offset * 60;
|
||||
return new Date(date.getTime() + offset * 60000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format date
|
||||
*
|
||||
* @param {Date | number | string} date Date or timestamp to convert to string
|
||||
* @param {string} format How date string will be formatted, default: `YYYY-MM-DD H:mm A`
|
||||
* @param tz Override timezone, can be either string or number
|
||||
* @return {string} Formated date
|
||||
*/
|
||||
export function formatDate(date: Date | number | string, format = 'YYYY-MM-DD H:mm '): string {
|
||||
export function formatDate(date: Date | number | string, format = 'YYYY-MM-DD H:mm', tz?: any): string {
|
||||
if(typeof date == 'number' || typeof date == 'string') date = new Date(date);
|
||||
|
||||
function day(num: number): string {
|
||||
@ -85,9 +90,8 @@ export function formatDate(date: Date | number | string, format = 'YYYY-MM-DD H:
|
||||
}
|
||||
|
||||
function timezone(date: Date): string {
|
||||
const formatter = new Intl.DateTimeFormat('en-US', {timeZoneName: 'short'});
|
||||
const formattedDate = formatter.format(date);
|
||||
return formattedDate.split(' ').pop() || '';
|
||||
return new Intl.DateTimeFormat('en-US', {timeZoneName: 'short'})
|
||||
.format(date).split(' ').pop() || '';
|
||||
}
|
||||
|
||||
return format
|
||||
@ -120,16 +124,28 @@ export function formatDate(date: Date | number | string, format = 'YYYY-MM-DD H:
|
||||
.replaceAll('ss', date.getSeconds().toString().padStart(2, '0'))
|
||||
.replaceAll('s', date.getSeconds().toString())
|
||||
// Millisecond
|
||||
.replaceAll('SSS', date.getMilliseconds().toString())
|
||||
.replaceAll('SS', date.getMilliseconds().toString().slice(0, 1))
|
||||
.replaceAll('SSS', date.getMilliseconds().toString().padEnd(3, '0'))
|
||||
.replaceAll('SS', date.getMilliseconds().toString().slice(0, 1).padEnd(2, '0'))
|
||||
.replaceAll('S', date.getMilliseconds().toString()[0])
|
||||
// Period/Meridian (AM/PM)
|
||||
.replaceAll('A', date.getHours() >= 12 ? 'PM' : 'AM')
|
||||
.replaceAll('a', date.getHours() >= 12 ? 'pm' : 'am')
|
||||
// Timezone
|
||||
.replaceAll('ZZ', tzOffset(date.getTimezoneOffset()).replace(':', ''))
|
||||
.replaceAll('Z', tzOffset(date.getTimezoneOffset()))
|
||||
.replaceAll('z', timezone(date));
|
||||
.replaceAll('ZZ', tzOffset(isNaN(tz) ? date.getTimezoneOffset() : tz).replace(':', ''))
|
||||
.replaceAll('Z', tzOffset(isNaN(tz) ? date.getTimezoneOffset() : tz))
|
||||
.replaceAll('z', typeof tz == 'string' ? tz : timezone(date));
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a function immediately & repeat every x ms
|
||||
*
|
||||
* @param {() => any} fn Callback function
|
||||
* @param {number} interval Repeat in ms
|
||||
* @return {number} Clear Interval ID
|
||||
*/
|
||||
export function instantInterval(fn: () => any, interval: number) {
|
||||
fn();
|
||||
return setInterval(fn, interval);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user