-
-
Save vicalejuri/3e4b26e36aff452a414bd88a8461aa34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Read a ISO string | |
*/ | |
export const fromISOString = (str: string) => { | |
return Date.parse(str); | |
}; | |
/** Convert from MilliTimestamp (javascript default) to unix */ | |
export const toUnixTimestamp = (milliTimestamp: number): UnixTimestamp => { | |
return (milliTimestamp / 1000) as UnixTimestamp; | |
}; | |
/* From Unix timestamp to DMY(internationally popular) string date */ | |
export function toDMYDateString(date: UnixTimestamp): string; | |
export function toDMYDateString(date: Date): string; | |
export function toDMYDateString(date: UnixTimestamp | Date): string { | |
switch (typeof date) { | |
case "number": | |
return new Date(date * 1000).toLocaleDateString("pt-BR"); | |
case "object": | |
return date.toLocaleDateString("pt-BR"); | |
} | |
} | |
/* From Unix timestamp to readable string date */ | |
export function toReadableDateString(date: UnixTimestamp): string; | |
export function toReadableDateString(date: Date): string; | |
export function toReadableDateString(date: UnixTimestamp | Date): string { | |
const options: Intl.DateTimeFormatOptions = { | |
weekday: "long", | |
year: "numeric", | |
month: "long", | |
day: "numeric", | |
}; | |
switch (typeof date) { | |
case "number": | |
return new Date(date * 1000).toLocaleDateString("en-US", options); | |
case "object": | |
return date.toLocaleDateString("en-US", options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment