more time utils
All checks were successful
Build / Build NPM Project (push) Successful in 38s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 2m27s

This commit is contained in:
Zakary Timson 2024-11-14 23:29:58 -05:00
parent 49a2df8cd4
commit fa66820c76
3 changed files with 31 additions and 19 deletions

View File

@ -6,14 +6,10 @@
</head> </head>
<body> <body>
<script type="module"> <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' + console.log(formatDate(new Date(), 'HH:mm:ss z'));
'34,,true,,,,,,,,system,2024-11-09T12:06:50.023Z,system,2024-11-09T17:44:42.512Z\n' + console.log(formatDate(adjustTz(new Date(), 0), 'HH:mm:ss z', 'UTC'));
'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));
</script> </script>
</body> </body>
</html> </html>

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.22.7", "version": "0.22.8",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@ -1,5 +1,3 @@
import {strSplice} from './string.ts';
/** /**
* Like setInterval but will adjust the timeout value to account for runtime * Like setInterval but will adjust the timeout value to account for runtime
* @param {Function} cb Callback function that will be ran * @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 * Format date
* *
* @param {Date | number | string} date Date or timestamp to convert to string * @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 {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 * @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); if(typeof date == 'number' || typeof date == 'string') date = new Date(date);
function day(num: number): string { 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 { function timezone(date: Date): string {
const formatter = new Intl.DateTimeFormat('en-US', {timeZoneName: 'short'}); return new Intl.DateTimeFormat('en-US', {timeZoneName: 'short'})
const formattedDate = formatter.format(date); .format(date).split(' ').pop() || '';
return formattedDate.split(' ').pop() || '';
} }
return format 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('ss', date.getSeconds().toString().padStart(2, '0'))
.replaceAll('s', date.getSeconds().toString()) .replaceAll('s', date.getSeconds().toString())
// Millisecond // Millisecond
.replaceAll('SSS', date.getMilliseconds().toString()) .replaceAll('SSS', date.getMilliseconds().toString().padEnd(3, '0'))
.replaceAll('SS', date.getMilliseconds().toString().slice(0, 1)) .replaceAll('SS', date.getMilliseconds().toString().slice(0, 1).padEnd(2, '0'))
.replaceAll('S', date.getMilliseconds().toString()[0]) .replaceAll('S', date.getMilliseconds().toString()[0])
// Period/Meridian (AM/PM) // Period/Meridian (AM/PM)
.replaceAll('A', date.getHours() >= 12 ? 'PM' : 'AM') .replaceAll('A', date.getHours() >= 12 ? 'PM' : 'AM')
.replaceAll('a', date.getHours() >= 12 ? 'pm' : 'am') .replaceAll('a', date.getHours() >= 12 ? 'pm' : 'am')
// Timezone // Timezone
.replaceAll('ZZ', tzOffset(date.getTimezoneOffset()).replace(':', '')) .replaceAll('ZZ', tzOffset(isNaN(tz) ? date.getTimezoneOffset() : tz).replace(':', ''))
.replaceAll('Z', tzOffset(date.getTimezoneOffset())) .replaceAll('Z', tzOffset(isNaN(tz) ? date.getTimezoneOffset() : tz))
.replaceAll('z', timezone(date)); .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);
} }
/** /**