https://github.com/chipmunktail/datecounter
A node package , Format date for article or news
https://github.com/chipmunktail/datecounter
es6 jslibrary npm
Last synced: 2 months ago
JSON representation
A node package , Format date for article or news
- Host: GitHub
- URL: https://github.com/chipmunktail/datecounter
- Owner: chipmunktail
- Created: 2018-07-06T05:15:31.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-22T10:18:40.000Z (almost 8 years ago)
- Last Synced: 2025-04-06T02:14:45.880Z (about 1 year ago)
- Topics: es6, jslibrary, npm
- Language: JavaScript
- Size: 26.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[中文](https://github.com/hjdtl/dateCounter/blob/master/README.zh.md) | [English](https://github.com/hjdtl/dateCounter)
# Datecounter
A tool class library that changes the time format
Format date for articles, news or comments
## Usage
### Parameter
Datecounter receive an **object parameter** , and return a **counter** :
| key | default/type | describe | required |
| - | :-: | :- | :-: |
| date | none/Date| Expected is Date Object or timestamp | yes |
| countDay | 7/number | Countdown days | no |
| i18n | 'zh'/string | Language | no |
| raw | none/boolean | Raw data | no |
### Essentials
```javascript
import Datecounter from 'Datecounter'
const counter=new Datecounter({countDay: 10, i18n: 'en'})
counter(new Date())
```
### Vue
```javascript
// for Vue
import Vue from 'vue'
import {vDatecounter} from 'Datecounter'
Vue.use(vDatecounter, {countDay: 10, i18n: 'en'})
/************************* Global method *************************/
this.$datecounter(new Date())
/************************* Local method *************************/
const counter=new this.$Datecounter({countDay: 10, i18n: 'en'})
counter(new Date())
```
### Example
```javascript
import Datecounter from 'Datecounter'
var now = new Date() // now : Thu Jul 05 2018 09:20:00
var twoMinutesAgo = now - (2 * 60 * 1000)
var twoHoursAgo = now - (2 * 60 * 60 * 1000)
var fiveDaysAgo = now - (5 * 24 * 60 * 60 * 1000)
var eightDaysAgo = now - (8 * 24 * 60 * 60 * 1000)
var january = now - (180 * 24 * 60 * 60 * 1000)
var aYearsAgo = now - (365 * 24 * 60 * 60 * 1000)
/************************* Default usage *************************/
const counter1=new Datecounter({i18n: 'en'})
counter1(now) // return : 0 minutes ago
counter1(fiveDaysAgo) // return : 5 days 0 hours ago
counter1(january) // return : January 6, 9:20
counter1(aYearsAgo) // return : July 5, 2017 9:20
/************************* Countdown days *************************/
const counter2=new Datecounter({countDay: 10, i18n: 'en'})
const counter3=new Datecounter({countDay: 7, i18n: 'en'})
counter2(eightDaysAgo) // return : 8 days 0 hours ago
counter3(eightDaysAgo) // return : June 27, 9:20
/************************* i18n *************************/
/* langList = ['zh', 'en', 'jp', 'ru', 'kr', 'fr', 'vi'] */
/* Now noly have ['zh', 'en', 'jp'] , other language are being developed */
const counter_zh=new Datecounter({i18n: 'zh'})
const counter_en=new Datecounter({i18n: 'en'})
const counter_jp=new Datecounter({i18n: 'jp'})
counter_zh(fiveDaysAgo) // return : 5天0小时前
counter_en(fiveDaysAgo) // return : 5 days 0 hours ago
counter_jp(fiveDaysAgo) // return : 5天0時間前
```
### Get raw data
```javascript
/* If you need raw data */
/* There are four kinds of data types in all ['history', 'countDown_day', 'countDown_hour', 'countDown_minute'] */
const count_raw=new Datecounter({raw: true})
count_raw(twoMinutesAgo)// return : {type: "countDown_minute", minute: 2}
count_raw(twoHoursAgo) // return : {type: "countDown_hour", hour: "2", minute: "0"}
count_raw(fiveDaysAgo) // return : {type: "countDown_day", day: "5", hour: "0"}
count_raw(january) // return : {type: "history", year: null, month: 1, date: 6, hour: 9, minute: 20 }
count_raw(aYearsAgo) // return : {type: "history", year: 2017, month: 7, date: 6, hour: 9, minute: 20 }
```
---
If you are good at these languages ['en', 'jp', 'ru', 'kr', 'fr', 'vi'], maybe you can help me.😄