Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created February 28, 2024 18:56
Show Gist options
  • Save kellenmace/eceef05aaeacae525afe251c3c3b4842 to your computer and use it in GitHub Desktop.
Save kellenmace/eceef05aaeacae525afe251c3c3b4842 to your computer and use it in GitHub Desktop.
import { formatDistance, parseISO } from 'date-fns';
// Example string date
const dateString: string = "2023-05-23";
// Parse the string date into a JavaScript Date object
const date: Date = parseISO(dateString);
// Get the current date
const currentDate: Date = new Date();
// Calculate the relative date string
const relativeDateString: string = formatDistance(date, currentDate, { addSuffix: true });
console.log(`Relative date: ${relativeDateString}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment