UNPKG

last-seen-ago

Version:

highly customizable real time to last seen like facebook,twitter,whatsap...

153 lines (117 loc) 5.19 kB
# last-seen-ago [![npm version](https://img.shields.io/npm/v/last-seen-ago.svg?style=flat-square)](https://www.npmjs.com/package/last-seen-ago) `last-seen-ago` is a zero-dependency module that highly customizable real time to last seen like [Facebook](https://facebook.com),[Twitter](https://twitter.com),[Instagram](https://instagram.com)... It is a simple one line code ,Its automatically converts to given timestamp in the right units like seconds, minutes, hours, days,weeks, years using `getLastSeen()` method. Examples: * Juts Now * 45 seconds * one minute ago * 15 minutes ago * an hour ago * 2 hrs ago * yesterday * 2 days ago * a week ago * 2 weeks ago *## Installation ``` npm install last-seen-ago --save ``` ## Usage As early as possible in your application, require and configure `last seen`.Check it following [Rules](#rules). ###getLastSeen() ```js //require last-seen-ago module const lastSeenAgo = require("last-seen-ago"); //for example your real time timestamp 1602050284 const realTime = 1602050284; //get exact last seen according to time const lastSeen = lastSeenAgo.getLastSeen(realTime) //print output will be "34 minutes ago" console.log(lastSeen) ``` ### Rules The parsing timstamp support the following rules: - To `timestamp` value should be `double` data type. - To get the `timestamp` as `seconds`. [For example](#) - To get the timestamp `Date.now()` method can be called. - Note that this method returns the timestamp in `milliseconds`. - To get the timestamp as seconds we can `divide` it by `1000`. ### Given are the following formats: - Format type 1 with backslashes (/) as the separator characters. - Format type 2 with dashes (-) as the separator characters. - Format type 3 with dots (.) as the separator characters. - Format type 4 with spaces ( ) as the separator characters. Type | Example ---- | ------- `YYYY-MM-DD` or `yyyy-mm-dd` | `2020-10-12` `YYYY-DD-MM` or `YYYY-DD-MM` | `2020-12-10` `MM-DD-YYYY` or `mm-dd-yyyy` | `10-12-2020` `DD-MM-YYYY` or `DD-MM-YYYY` | `12-10-2020` `YYYY-MM-DD HH:MM:SS` or `yyyy-mm-dd hh:mm:ss` | `2020-10-12 11:26:1` `YYYY-DD-MM HH:MM:SS` or `YYYY-DD-MM hh:mm:ss` | `2020-12-10 11:26:1` `MM-DD-YYYY HH:MM:SS` or `mm-dd-yyyy hh:mm:ss` | `10-12-2020 11:26:1` `DD-MM-YYYY HH:MM:SS` or `DD-MM-YYYY hh:mm:ss` | `12-10-2020 11:26:1` `YYYY.MM.DD` or `yyyy.mm.dd` | `2020.10.12` `YYYY.DD.MM` or `YYYY.DD.MM` | `2020.12.10` `MM.DD.YYYY` or `mm.dd.yyyy` | `10.12.2020` `DD.MM.YYYY` or `DD.MM.YYYY` | `12.10.2020` `YYYY.MM.DD HH:MM:SS` or `yyyy.mm.dd hh:mm:ss` | `2020.10.12 11:26:1` `YYYY.DD.MM HH:MM:SS` or `YYYY.DD.MM hh:mm:ss` | `2020.12.10 11:26:1` `MM.DD.YYYY HH:MM:SS` or `mm.dd.yyyy hh:mm:ss` | `10.12.2020 11:26:1` `DD.MM.YYYY HH:MM:SS` or `DD.MM.YYYY hh:mm:ss` | `12.10.2020 11:26:1` `YYYY/MM/DD` or `yyyy/mm/dd` | `2020/10/12` `YYYY/DD/MM` or `YYYY/DD/MM` | `2020/12/10` `MM/DD/YYYY` or `mm/dd/yyyy` | `10/12/2020` `DD/MM/YYYY` or `DD/MM/YYYY` | `12/10/2020` `YYYY/MM/DD HH:MM:SS` or `yyyy/mm/dd hh:mm:ss` | `2020/10/12 11:26:1` `YYYY/DD/MM HH:MM:SS` or `YYYY/DD/MM hh:mm:ss` | `2020/12/10 11:26:1` `MM/DD/YYYY HH:MM:SS` or `mm/dd/yyyy hh:mm:ss` | `10/12/2020 11:26:1` `DD/MM/YYYY HH:MM:SS` or `DD/MM/YYYY hh:mm:ss` | `12/10/2020 11:26:1` `YYYY MM DD` or `yyyy mm dd` | `2020 10 12` `YYYY DD MM` or `YYYY DD MM` | `2020 12 10` `MM DD YYYY` or `mm dd yyyy` | `10 12 2020` `DD MM YYYY` or `DD MM YYYY` | `12 10 2020` `YYYY MM DD HH:MM:SS` or `yyyy mm dd hh:mm:ss` | `2020 10 12 11:26:1` `YYYY DD MM HH:MM:SS` or `YYYY DD MM hh:mm:ss` | `2020 12 10 11:26:1` `MM DD YYYY HH:MM:SS` or `mm dd yyyy hh:mm:ss` | `10 12 2020 11:26:1` `DD MM YYYY HH:MM:SS` or `DD MM YYYY hh:mm:ss` | `12 10 2020 11:26:1` ### Some Short formats Type | Example ---- | ----- `Y/m/d H:m:s` | `2020/10/12 11:26:1` `Y/d/m H:m:s` | `2020/12/10 11:26:1` `m/d/Y H:m:s` | `10/12/2020 11:26:1` `d/m/Y H:m:s` | `12/10/2020 11:26:1` `Y-m-d H:m:s` | `2020-10-12 11:26:1` `Y-d-m H:m:s` | `2020-12-10 11:26:1` `m-d-Y H:m:s` | `10-12-2020 11:26:1` `d-m-Y H:m:s` | `12-10-2020 11:26:1` `Y.m.d H:m:s` | `2020.10.12 11:26:1` `Y.d.m H:m:s` | `2020.12.10 11:26:1` `m.d.Y H:m:s` | `10.12.2020 11:26:1` `d.m.Y H:m:s` | `12.10.2020 11:26:1` ###getFormat() ```js //require last-seen-ago module const lastSeenAgo = require("last-seen-ago"); //if timestamp in 1602495292171(miliseconds),then converts timestamp in seconds divide it by 1000. const realTime = 1602495292171; //if timestamp in 1602495292 (seconds). const realTime = 1602495292; //get exact last seen according to timestmap and specific format. const lastSeen = lastSeenAgo.getFormat(realTime,'YYYY-MM-DD') //print output will be 2020-12-10" console.log(lastSeen) ``` ### Future Plan More features added soon like. <!-- When given `timestamp` with Specifice format like `YYYY/MM/DD` or `yyyy/mm/dd`,`getFormat()` method produces the corresponding output. ```js lastSeenAgo.getFormat(1602050284,'YYYY-MM-DD') //print output 2020-10-09 ``` --> ## License [MIT](LICENSE)