Skip to content

Commit

Permalink
Added a util function to convert from unix timestamp to DateArray (#196)
Browse files Browse the repository at this point in the history
A new util function is created to convert from unix timestamp to DateArray format

Eg: ics.convertTimestampToArray(1628752185560) -> [ 2021, 8, 12, 12, 39 ]
  • Loading branch information
ramseth001 authored Dec 4, 2021
1 parent f9e7555 commit b2df02e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ export function createEvent(attributes: EventAttributes): ReturnObject;
export function createEvents(events: EventAttributes[], callback: NodeCallback): void;

export function createEvents(events: EventAttributes[]): ReturnObject;

export function convertTimestampToArray(timestamp: Number, inputType: String): DateArray;
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ function catenateEvents(accumulator, { error, value }, idx) {
return accumulator
}

export function convertTimestampToArray(timestamp, inputType = 'local') {
const dateArray = [];
const d = new Date(timestamp);
dateArray.push(inputType === 'local' ? d.getFullYear() : d.getUTCFullYear());
dateArray.push((inputType === 'local' ? d.getMonth() : d.getUTCMonth()) + 1);
dateArray.push(inputType === 'local' ? d.getDate() : d.getUTCDate());
dateArray.push(inputType === 'local' ? d.getHours() : d.getUTCHours());
dateArray.push(inputType === 'local' ? d.getMinutes() : d.getUTCMinutes());
return dateArray;
}

export function createEvent (attributes, cb) {
if (!attributes) { Error('Attributes argument is required') }

Expand Down Expand Up @@ -103,4 +114,4 @@ export function createEvents (events, cb) {
}

return cb(error, value)
}
}

0 comments on commit b2df02e

Please sign in to comment.