Skip to content

Instantly share code, notes, and snippets.

@pupato13
Created September 22, 2020 21:00
Show Gist options
  • Save pupato13/d7a45f48e8ba5ee1e706c5383261703b to your computer and use it in GitHub Desktop.
Save pupato13/d7a45f48e8ba5ee1e706c5383261703b to your computer and use it in GitHub Desktop.
import { parse, format } from "date-fns";
export function convertStringToDate(dateValue: string): Date {
const parsedDate = parse(dateValue, "dd/MM/yyyy", new Date());
const day = parsedDate.getDate();
const month = parsedDate.getMonth();
const year = parsedDate.getFullYear();
return new Date(year, month, day, 23, 59, 59);
}
export function convertDateToString(dateValue: Date): string {
const day = `0${dateValue.getDate()}`.slice(-2);
const month = `0${dateValue.getMonth() + 1}`.slice(-2);
const year = dateValue.getFullYear();
return `${day}/${month}/${year}`;
}
export function getDateTimestamp(data: string): number {
return Date.parse(data);
}
export function getFormattedDate(
data: Date | number,
dateFormat: string,
): string {
return format(data, dateFormat);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment