Skip to content

Instantly share code, notes, and snippets.

@abigmiu
Created December 23, 2022 11:49
Show Gist options
  • Save abigmiu/072e8392f22f1c1cc51ede7cbeb04fb7 to your computer and use it in GitHub Desktop.
Save abigmiu/072e8392f22f1c1cc51ede7cbeb04fb7 to your computer and use it in GitHub Desktop.
export function timeStampTo13(value: number) {
if (`${value}`.length === 10) {
return value * 1000;
}
return value;
}
export function timeStampTo10(value: number) {
if (`${value}`.length === 13) {
return Math.floor(value / 1000);
}
return value;
}
export function transformTimeStampData<T extends Record<string, any>, K extends keyof T>(
data: Array<T> | T,
keys: Array<K>,
isToServer = true,
) {
const ary = Array.isArray(data) ? data : [data];
ary.forEach((item) => {
keys.forEach((key) => {
if (isToServer) {
(item[key] as number) = timeStampTo10(item[key] as number);
} else {
(item[key] as number) = timeStampTo13(item[key] as number);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment