utils-datetime
Advanced tools
Comparing version
{ | ||
"name": "utils-datetime", | ||
"version": "0.9.5", | ||
"description": "Easily handle and work with dates and times.", | ||
"main": "dist/index.js", | ||
"devDependencies": { | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.2.2" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"start": "ts-node src/index.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ingferchorojas/utils-datetime.git" | ||
}, | ||
"keywords": ["date", "time", "timezone", "datetime", "backend"], | ||
"author": "Fernando Rojas", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/ingferchorojas/easy-datetime/issues" | ||
}, | ||
"homepage": "https://github.com/ingferchorojas/easy-datetime#readme" | ||
"name": "utils-datetime", | ||
"version": "0.9.6", | ||
"description": "Easily handle and work with dates and times.", | ||
"main": "dist/index.js", | ||
"devDependencies": { | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.2.2" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"start": "ts-node src/index.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ingferchorojas/utils-datetime.git" | ||
}, | ||
"keywords": [ | ||
"date", | ||
"time", | ||
"timezone", | ||
"datetime", | ||
"backend" | ||
], | ||
"author": "Fernando Rojas", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/ingferchorojas/easy-datetime/issues" | ||
}, | ||
"homepage": "https://github.com/ingferchorojas/easy-datetime#readme" | ||
} |
146
README.md
@@ -12,8 +12,9 @@ # utils-datetime | ||
``` | ||
## Import | ||
```typescript | ||
import { | ||
getCurrentDate, | ||
timeTransform, | ||
import { | ||
getCurrentDate, | ||
timeTransform, | ||
dateTimeFormat, | ||
@@ -23,4 +24,4 @@ dateDiff, | ||
currentWeekNumber, | ||
formatMilliseconds | ||
} from 'utils-datetime' | ||
formatMilliseconds, | ||
} from "utils-datetime"; | ||
``` | ||
@@ -30,11 +31,19 @@ | ||
- dateDiff = (dates: DateInterface, timePeriod: TimePeriod): is a function that calculates the time difference between two dates in milliseconds, seconds, minutes, hours, days, weeks, months, or years, depending on the specified time period. | ||
- dateDiff = (dates: DateInterface, timePeriod: TimePeriod): is a function that calculates the time difference between two dates in milliseconds, seconds, minutes, hours, days, weeks, months, or years, depending on the specified time period. | ||
```typescript | ||
interface DateInterface { | ||
start: Date | string | ||
end: Date | string | ||
start: Date | string; | ||
end: Date | string; | ||
} | ||
type TimePeriod = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years' | ||
type TimePeriod = | ||
| "milliseconds" | ||
| "seconds" | ||
| "minutes" | ||
| "hours" | ||
| "days" | ||
| "weeks" | ||
| "months" | ||
| "years"; | ||
``` | ||
@@ -44,29 +53,40 @@ | ||
const dates = { | ||
start: new Date('2022-01-01'), | ||
end: new Date('2023-01-01') | ||
} | ||
start: new Date("2022-01-01"), | ||
end: new Date("2023-01-01"), | ||
}; | ||
const result = dateDiff(dates, 'days') | ||
console.log(result) | ||
const result = dateDiff(dates, "days"); | ||
console.log(result); | ||
// 365 | ||
``` | ||
- getCurrentDate = (timezone: string = 'UTC'): Gets the current date in the specified time zone. | ||
- `getCurrentDate(timezone: string = 'UTC', offset: string = '+0')`: Returns the current date in the specified time zone, with optional time offset adjustment. | ||
```typescript | ||
const now = getCurrentDate('America/Asuncion') | ||
console.log(now) | ||
const now = getCurrentDate("America/Asuncion"); | ||
console.log(now); | ||
// 2023-10-23T22:14:49.876Z | ||
const later = getCurrentDate("America/Asuncion", "1h"); | ||
console.log(later); | ||
// 2023-10-23T23:14:49.876Z | ||
``` | ||
- timeTransform = (date: Date, timeDelta: ITimeDelta): Performs addition or subtraction operations on a date according to the provided options. | ||
Supported `offset` formats: | ||
Milliseconds: `"+1000"`, `"-2000"` | ||
Seconds: `"30s"`, `"-45s"` | ||
Minutes: `"15m"`, `"-10m"` | ||
Hours: `"1h"`, `"-3h"` | ||
- timeTransform = (date: Date, timeDelta: ITimeDelta): Performs addition or subtraction operations on a date according to the provided options. | ||
```typescript | ||
interface ITimeDelta { | ||
year?: number, | ||
month?: number, | ||
day?: number, | ||
hour?: number, | ||
minute?: number, | ||
second?: number | ||
year?: number; | ||
month?: number; | ||
day?: number; | ||
hour?: number; | ||
minute?: number; | ||
second?: number; | ||
} | ||
@@ -77,22 +97,23 @@ ``` | ||
const options = { | ||
year: -1, | ||
day: 1, | ||
hour: 1, | ||
minute: -3, | ||
second: 120, | ||
month: -1 | ||
} | ||
const now = getCurrentDate('America/Asuncion') | ||
const transform = timeTransform(now, options) | ||
console.log(transform) | ||
year: -1, | ||
day: 1, | ||
hour: 1, | ||
minute: -3, | ||
second: 120, | ||
month: -1, | ||
}; | ||
const now = getCurrentDate("America/Asuncion"); | ||
const transform = timeTransform(now, options); | ||
console.log(transform); | ||
// 2022-09-25T00:26:06.078Z | ||
``` | ||
- dateTimeFormat = (date: Date, template: string, language: string = 'en-EN'): Formats the date according to the specified template and language. | ||
- dateTimeFormat = (date: Date, template: string, language: string = 'en-EN'): Formats the date according to the specified template and language. | ||
```typescript | ||
const now = getCurrentDate('America/Asuncion') | ||
const template = "Today is %DAY%, %MONTH% %DD%rd of the year %YYYY%, the time is %H%:%MI% %A%" | ||
const format = dateTimeFormat(now, template, 'es-EN') | ||
console.log(format) | ||
const now = getCurrentDate("America/Asuncion"); | ||
const template = | ||
"Today is %DAY%, %MONTH% %DD%rd of the year %YYYY%, the time is %H%:%MI% %A%"; | ||
const format = dateTimeFormat(now, template, "es-EN"); | ||
console.log(format); | ||
// Today is Monday, October 23rd of the year 2023, the time is 10:20 PM. | ||
@@ -102,6 +123,7 @@ ``` | ||
```typescript | ||
const now = getCurrentDate('America/Asuncion') | ||
const template = "Today is %DAY%, %MONTH% %DD%rd of the year %YYYY%, the time is %HH%:%MI%:%SS% with %MS% milliseconds" | ||
const format = dateTimeFormat(now, template, 'es-EN') | ||
console.log(format) | ||
const now = getCurrentDate("America/Asuncion"); | ||
const template = | ||
"Today is %DAY%, %MONTH% %DD%rd of the year %YYYY%, the time is %HH%:%MI%:%SS% with %MS% milliseconds"; | ||
const format = dateTimeFormat(now, template, "es-EN"); | ||
console.log(format); | ||
// Today is Monday, October 23rd of the year 2023, the time is 22:20:20 with 345 milliseconds. | ||
@@ -111,6 +133,6 @@ ``` | ||
```typescript | ||
const now = getCurrentDate('America/Asuncion') | ||
const template = "%DD%/%MM%/%YYYY% %HH%:%MI%" | ||
const format = dateTimeFormat(now, template) | ||
console.log(format) | ||
const now = getCurrentDate("America/Asuncion"); | ||
const template = "%DD%/%MM%/%YYYY% %HH%:%MI%"; | ||
const format = dateTimeFormat(now, template); | ||
console.log(format); | ||
// 23/10/2023 22:20 | ||
@@ -121,6 +143,6 @@ // %HH% 23h format | ||
```typescript | ||
const now = getCurrentDate('America/Asuncion') | ||
const template = "%DD%/%MM%/%YYYY% %H%:%MI% %A%" | ||
const format = dateTimeFormat(now, template) | ||
console.log(format) | ||
const now = getCurrentDate("America/Asuncion"); | ||
const template = "%DD%/%MM%/%YYYY% %H%:%MI% %A%"; | ||
const format = dateTimeFormat(now, template); | ||
console.log(format); | ||
// 23/10/2023 10:20 PM | ||
@@ -130,28 +152,28 @@ // %H% 12h format | ||
- const isLeapYear = (yearInterface: YearInterface): checks if a given year (after 1582) is a leap year according to the Gregorian calendar. | ||
- const isLeapYear = (yearInterface: YearInterface): checks if a given year (after 1582) is a leap year according to the Gregorian calendar. | ||
```typescript | ||
type YearInterface = number | string | ||
type YearInterface = number | string; | ||
``` | ||
```typescript | ||
const leapYear = isLeapYear(2024) | ||
console.log(leapYear) | ||
const leapYear = isLeapYear(2024); | ||
console.log(leapYear); | ||
// true | ||
``` | ||
- const currentWeekNumber = (date: Date | string): takes a Date or date string, calculates and returns the ISO week number, assuming a Monday start. | ||
- const currentWeekNumber = (date: Date | string): takes a Date or date string, calculates and returns the ISO week number, assuming a Monday start. | ||
```typescript | ||
const result = currentWeekNumber('2023-02-27') | ||
console.log(result) | ||
const result = currentWeekNumber("2023-02-27"); | ||
console.log(result); | ||
// 9 | ||
``` | ||
- const formatMilliseconds = (milliseconds: number | string, includeMS: boolean = false): takes a time value in milliseconds (as a string or number) and returns it as HH:MM:SS. If includeMS is true, it adds milliseconds as HH:MM:SS.MS | ||
- const formatMilliseconds = (milliseconds: number | string, includeMS: boolean = false): takes a time value in milliseconds (as a string or number) and returns it as HH:MM:SS. If includeMS is true, it adds milliseconds as HH:MM:SS.MS | ||
```typescript | ||
const result = formatMilliseconds(3661000) | ||
console.log(result) | ||
const result = formatMilliseconds(3661000); | ||
console.log(result); | ||
// 01:01:01 | ||
``` | ||
``` |
@@ -1,28 +0,55 @@ | ||
const getCurrentDate = (timezone: string = 'UTC'): Date => { | ||
const now = new Date() | ||
const getCurrentDate = ( | ||
timezone: string = "UTC", | ||
offset: string = "+0" | ||
): Date => { | ||
const now = new Date(); | ||
const options: Intl.DateTimeFormatOptions = { | ||
timeZone: timezone, | ||
year: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
hourCycle: 'h23' | ||
} | ||
const options: Intl.DateTimeFormatOptions = { | ||
timeZone: timezone, | ||
year: "numeric", | ||
month: "2-digit", | ||
day: "2-digit", | ||
hour: "2-digit", | ||
minute: "2-digit", | ||
second: "2-digit", | ||
hourCycle: "h23", | ||
}; | ||
const formattedDate = new Intl.DateTimeFormat('en-US', options).formatToParts() | ||
const { year, month, day, hour, minute, second } = Object.fromEntries(formattedDate.map(part => [part.type, part.value])) | ||
const milliseconds = now.getMilliseconds() | ||
const millisecondsString = milliseconds.toString().padStart(3, '0') // Asegura que siempre tenga tres dígitos | ||
const dateString = `${year}-${month}-${day}T${hour}:${minute}:${second}.${millisecondsString}Z` | ||
const formattedDate = new Intl.DateTimeFormat( | ||
"en-US", | ||
options | ||
).formatToParts(); | ||
const { year, month, day, hour, minute, second } = Object.fromEntries( | ||
formattedDate.map((part) => [part.type, part.value]) | ||
); | ||
return new Date(dateString) | ||
} | ||
const milliseconds = now.getMilliseconds(); | ||
const millisecondsString = milliseconds.toString().padStart(3, "0"); | ||
export { | ||
getCurrentDate | ||
} | ||
const dateString = `${year}-${month}-${day}T${hour}:${minute}:${second}.${millisecondsString}Z`; | ||
const baseDate = new Date(dateString); | ||
//Parse offset ("+1000", "-2000", "1h", "30m", "45s") | ||
const regex = /^([+-]?)(\d+)(ms|s|m|h)?$/; | ||
const match = offset.match(regex); | ||
let offsetMs = 0; | ||
if (match) { | ||
const [, sign, valueStr, unit = "ms"] = match; | ||
const value = parseInt(valueStr, 10); | ||
const multiplier = | ||
unit === "h" | ||
? 3600000 | ||
: unit === "m" | ||
? 60000 | ||
: unit === "s" | ||
? 1000 | ||
: 1; | ||
offsetMs = value * multiplier; | ||
if (sign === "-") offsetMs *= -1; | ||
} | ||
return new Date(baseDate.getTime() + offsetMs); | ||
}; | ||
export { getCurrentDate }; |
171
15.54%13882
-41.6%11
-59.26%226
-47.69%