Skip to content

Instantly share code, notes, and snippets.

@Ebrahim-Ramadan
Created July 5, 2023 02:18
Show Gist options
  • Save Ebrahim-Ramadan/2c707d44696e55b54986f7454453dbed to your computer and use it in GitHub Desktop.
Save Ebrahim-Ramadan/2c707d44696e55b54986f7454453dbed to your computer and use it in GitHub Desktop.
A human-readable representation of a timestamp starting from the current time-Reactjs
import React, { Component } from 'react';
import { render } from 'react-dom';
import TimeAgo from 'react-timeago';
class App extends Component {
render() {
return (
<div>
<TimeAgo date={Date.now()} formatter={(value, unit, suffix) => {
switch (unit) {
case 'second':
return value + ' ' + unit + (value > 1 ? 's' : '') + ' ' + suffix;
case 'minute':
return value + ' ' + unit + (value > 1 ? 's' : '') + ' ' + suffix;
case 'hour':
return value + ' ' + unit + (value > 1 ? 's' : '') + ' ' + suffix;
case 'day':
return value + ' ' + unit + (value > 1 ? 's' : '') + ' ' + suffix;
case 'month':
return value + ' ' + unit + (value > 1 ? 's' : '') + ' ' + suffix;
case 'year':
return value + ' ' + unit + (value > 1 ? 's' : '') + ' ' + suffix;
case 'decade':
return value + ' ' + unit + (value > 1 ? 's' : '') + ' ' + suffix;
default:
return null; // Hide any other time units
}
}} />
</div>
);
}
}
render(<App />, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment