Skip to content

Commit

Permalink
Big update
Browse files Browse the repository at this point in the history
- add i10n
- add format tokens
- update custom formats
  • Loading branch information
Maxim Ponomarev committed Nov 11, 2013
1 parent eab804e commit f5cf758
Show file tree
Hide file tree
Showing 10 changed files with 513 additions and 98 deletions.
30 changes: 30 additions & 0 deletions .jshint
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
// Enforcing options
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"undef": true,
"unused": true,
"trailing": true,
"maxdepth": 3,
"maxstatements": 12,
"maxcomplexity": 5,

// Relaxing options
"boss": true,
"eqnull": true,
"expr": true,
"lastsemic": true,
"sub": true,

// Environments
"maxerr": 5,
"browser": true,
"node": true
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
171 changes: 123 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,124 @@
# datef

**datef** is a Javascript date formatting library, both for browser and node.js.

##Features
## Features

* Can output: year, full year, month, day, hour, minutes, seconds and timezone — both zero padded and not
* Can output: year, full year, month, day, hour, minutes, seconds, milliseconds and timezone — both zero padded and not
* Simple localization
* Built-in formats and easyly extensible
* Doesn't mess with built-in prototypes
* Acts as node.js and requirejs/amd module, and have `.noConflict` method in case no module system is present (e.g. plain browser environment)
* Acts as node.js and requirejs/amd module, and have `.noConflict` method in case no module system is present (e.g. plain browser environment)
* No external dependencies
* Contains predefined formats for some ISO8601 date and time representation
* Thoroughly annotated with jsdoc, so if your IDE/editor supports it, you may never need interrupt yourself from code

##Usage
## Usage

**Node.js**
```bash
npm install datef
```
```js
var datef = require('datef');
datef('MM.YYYY');
```

**Require.js**
```js
require.config({
paths: {
'datef': 'path/to/datef'
}
})
require(['datef'], function(datef){
datef('MM.YYYY');
});
```

**Browser**
```html
<script src="datef.js"></script>
<script>
datef('MM.YYYY');
</script>
```

```javascript
// basic usage
datef('dd.MM.YY', new Date()); // "13.08.12"
### Basic usage

```js
datef('dd.MM.YY', new Date()); // "13.08.13"
datef('dd.MM.YY'); // second argument is optional; datef takes Date.now() if no date is provided

var d = new Date();
d.setFullYear(2045);
datef('dd.MM.YYYY', d); // "13.08.2045"
```

### Languages

**Node.js**
```js
datef.lang('ru');
datef('dd MMMM'); // 13 августа
```

**Require.js**
```js
require.config({
paths: {
'datef': 'path/to/datef',
'datef_ru': 'path/to/datef_lang/ru'
}
})

require(['datef', 'datef_ru'], function(datef){
datef.lang('ru');
datef('dd MMMM'); // 13 августа
});
```

**Browser**
```html
<script src="datef.js"></script>
<script src="lang/ru.js"></script>
<script>
datef.lang('ru');
datef('dd MMMM'); // 13 августа
</script>
```

### Custom formatters
```js
// predefined formats
datef.formatters.ISODateTimeTZ(); // "2012-08-13T15:01:29 -04:00"
datef.formatters(); // ['ISODate','ISOTime','ISODateTime','ISODateTimeTZ']
datef('ISODateTimeTZ', d); // "2013-08-13T15:01:29 -04:00"

// defining your own format
// defining your own simple format
datef.register('myFormat', 'd.M.YY');
datef.formatters.myFormat(); // "13.8.12"
// or as standalone function
var myFormat = datef.createFormatter('d.M.YY');
datef('myFormat', d); // "13.8.13"

// defining your own format with i10n
datef.register('myI10nFormat', {
'en': 'MMMM dd, DD',
'ru': 'DD, dd MMMM',
'default': 'dd MMMM'
});
datef('myI10nFormat', d); // "August 13, Tuesday"
datef.lang('ru');
datef('myI10nFormat', d); // "Вторник, 13 августа"
datef.lang('uk');
datef('myI10nFormat', d); // "13 жніўня"
```

### Cleaning global namespace

This is similar to [`Backbone.noConflict()`](http://backbonejs.org/#Utility-noConflict)

```html
<script>var datef = 'My very important data';</script>
<script src="js/datef.js"></script>
<script src="datef.js"></script>

<script>
console.log(typeof datef); // "function"
Expand All @@ -45,41 +127,34 @@ console.log(datef); // 'My very important data'
</script>
```

##Tokens
## Tokens

Full list of tokens possible in format string include:

**YYYY**: 4-digit year

**YY**: last 2 digit of year

**MM**: ISO8601-compatible number of month (i.e. zero-padded) in year (with January being 1st month)

**M**: number of month in year without zero-padding (with January being 1st month)

**dd**: zero-padded number of day in month

**d**: number of day in month

**hh**: zero-padded hour

**h**: hour

**mm**: zero-padded minutes

**m**: minutes

**ss**: zero-padded seconds

**s**: seconds

**Z**: time-zone in ISO8601-compatible format (i.e. "-04:00")

**ff**: zero-padded milliseconds, 3 digits

**f**: milliseconds

##Roadmap

* add ability to extend set of tokens
* add l10n capabilities
* **YYYY**: 4-digit year
* **YY**: last 2 digit of year
* **MMMM**: full name of month
* **MMM**: short name of month
* **MM**: ISO8601-compatible number of month (i.e. zero-padded) in year (with January being 1st month)
* **M**: number of month in year without zero-padding (with January being 1st month)
* **DD**: full name of day
* **D**: short name of day
* **dd**: zero-padded number of day in month
* **d**: number of day in month
* **HH**: zero-padded 24 hour time
* **H**: 24 hour time
* **hh**: zero-padded 12 hour time
* **h**: 12 hour time
* **mm**: zero-padded minutes
* **m**: minutes
* **ss**: zero-padded seconds
* **s**: seconds
* **ff**: zero-padded milliseconds, 3 digits
* **f**: milliseconds
* **A**: AM/PM
* **a**: am/pm
* **Z**: time-zone in ISO8601-compatible format (i.e. "-04:00")

## Roadmap

* add more languages
Loading

0 comments on commit f5cf758

Please sign in to comment.