Skip to content

Instantly share code, notes, and snippets.

@vicalejuri
Created May 20, 2022 19:20
Show Gist options
  • Save vicalejuri/3e4b26e36aff452a414bd88a8461aa34 to your computer and use it in GitHub Desktop.
Save vicalejuri/3e4b26e36aff452a414bd88a8461aa34 to your computer and use it in GitHub Desktop.
/**
* 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